Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,456 members, 7,816,056 topics. Date: Friday, 03 May 2024 at 01:24 AM

Help. Mysql Function Returning Booleans Instead Of Resourse - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help. Mysql Function Returning Booleans Instead Of Resourse (1133 Views)

Contest[closed] : Program A Function to find if a phrase. is in a string / Lets Start A Real Web-deveopment Course Here Html+css+javascript+php+mysql / DHTMLSQL - A Very Advanced Wrapper For Mysql Database! (2) (3) (4)

(1) (Reply) (Go Down)

Help. Mysql Function Returning Booleans Instead Of Resourse by Thirdwrist(m): 5:26pm On Jan 21, 2016
I have bee learning PHP for some time now, and covered a little ground in it. But all that has been in procedual style. So I decided to delve into OOP it being the most secure of both and more organized. Here is my problem:
<?php
class connect{
public $db_host= "localhost";
public $username="";
public $password= "";
public $db_name="";
public $dbc;
function db_connect(){
try{
$this->dbc= new PDO("mysql:host=$this->db_host; dbname=$this->db_name", $this->username, $this->password);
mysql_select_db($this->db_name);
}
catch (PDOException $e){
echo "failed to connect to database pdo ";
echo $e->getmessage();
exit();
}
return $this->dbc;
}
public $sql, $row, $result;
public function tester(){
if(mysql_connect())
{
$this->result=mysql_query("SELECT email FROM users WHERE id =1"wink;
$this->row=mysql_fetch_row($this->result);
$this->sql=$this->row['email'];
}
else{
$this->sql= "bah quannection";
}
return $this->sql;
}
}
$connection = new connect();
$connection->db_connect();
echo "you are an OOP genius";
$connection->tester();
echo $connection->sql;
?>
and it keeps outputting " you are an OOP genius
Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Experiments\gstosa\index.php on line 29
Re: Help. Mysql Function Returning Booleans Instead Of Resourse by sinequanon: 6:26pm On Jan 21, 2016
Warning? Why don't you ask it if it is a genius, too? cool

I am not a mysql programmer, but I suggest that your SELECT statement is failing.

This could be because the correct schema containing your table has not been specified, the connected user does not have authority to the table, the table name or a field has been misspelled, or id is alphanumeric.

You may want to call something like mysqli_error() or mysql_error() ?? to display what the error is.

1 Like

Re: Help. Mysql Function Returning Booleans Instead Of Resourse by Nobody: 6:30pm On Jan 21, 2016
you are mixing mysql and pdo functions, it doesn't work that way

1 Like

Re: Help. Mysql Function Returning Booleans Instead Of Resourse by sinequanon: 6:39pm On Jan 21, 2016
Jregz:
you are mixing mysql and pdo functions, it doesn't work that way

When will folk stop saying "it doesn't work that way"!! smiley

More explanation please.

It seems to me that the connection is successful, because the program gets to the point row=mysql_fetch_row, and it is complaining that the wrong parameter has been passed. This would appear to be because the SELECT has failed and is returning FALSE instead of a result set.

2 Likes

Re: Help. Mysql Function Returning Booleans Instead Of Resourse by Thirdwrist(m): 6:40pm On Jan 21, 2016
Jregz:
you are mixing mysql and pdo functions, it doesn't work that way
how then do I change that to pdo?
Re: Help. Mysql Function Returning Booleans Instead Of Resourse by Nobody: 8:02pm On Jan 21, 2016
sinequanon:


When will folk stop saying "it doesn't work that way"!! smiley

More explanation please.

It seems to me that the connection is successful, because the program gets to the point row=mysql_fetch_row, and it is complaining that the wrong parameter has been passed. This would appear to be because the SELECT has failed and is returning FALSE instead of a result set.

that was the answer to his problem, he connected with pdo and is trying to fetch data with myqsl* functions that he hasn't connected with, the place where he used mysql_connect should have his credentials, the way he checked for mysql_connect would only check if the function is available, nothing more

1 Like

Re: Help. Mysql Function Returning Booleans Instead Of Resourse by maekhel(m): 9:05pm On Jan 21, 2016
Jregz got here before me. That's what I would have said.

mixing mysql and pdo functions is bad practice and should never be encouraged.
Re: Help. Mysql Function Returning Booleans Instead Of Resourse by maekhel(m): 9:37pm On Jan 21, 2016
Thirdwrist:
how then do I change that to pdo?
I have updated your code

<?php
class connect{
public $db_host= "localhost";
public $username="";
public $password= "";
public $db_name="";
public $dbc;

function db_connect()
{
try{
$this->dbc = new PDO("mysql:host=$this->db_host; dbname=$this->db_name", $this->username, $this->password);
}
catch (PDOException $e){
echo "failed to connect to database pdo ";
echo $e->getmessage();
exit();
}

return $this->dbc;
}

public $sql, $row, $result;

public function tester()
{
if(mysql_connect()){
$this->stmt = $dbc->prepare("SELECT email FROM users WHERE id =:id"wink ;
$this->stmt->execute(["id" => 1]);

while($this->row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $this->row['name'];
}
}
}

$connection = new connect();
$connection->db_connect();
echo "you are an OOP genius";
$connection->tester();
echo $connection->sql;
?>

Note the updated code is not tested.

(1) (Reply)

Selling Of Some Of My Custom Programmed Application At A Rediculous Price / CcHUB Student Community For Students In Nigeria / Coding Competition

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 18
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.