Jump to content

[SOLVED] PHP MYSQL Query problem.. Simple solution, i just don't get it help please!!


physaux

Recommended Posts

Ok guys, i made a database with PHPMyAdmin. I connected to it using the following script:

$connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS); 
if (!$connection) {
	die("Database connection failed: " . mysql_error());
}

$db_select = mysql_select_db(DB_NAME,$connection);
if (!$db_select) {
	die("Database selection failed: " . mysql_error());
}
echo "yes you actually ran the code without errors";

I ran the php file, and i did NOT get any ERROR, except for the following text:

yes you actually ran the code without errors

So i know that is not the problem. I need help with the following:

 

I am trying to make a function that will search for a value in the database. I have a 1% clue what i am doing here, and that is probably what is wrong. Anyways, here is what i have so far:

function returndataforname($name){
$result = mysql_query("SELECT * FROM 'users'", $connection);
if (!$result) {
	die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
	if($row["name"] == $name){
		$returnvalue['language']=$row['language'];
		$returnvalue['name']=$row['name'];
		$returnvalue['password']=$row['password'];
		return $returnvalue;
	}
}
}

I have no clue what is going on. Could somebody please help me out, thank you so much!!

add this code inside the function

 

global $connection;

 

Ok i did, and i get this error:

 

Warning: mysql_query() expects parameter 2 to be resource, null given in /Users/.../Sites/test/includes/functions.php on line 87

Database query failed:

 

**Line 87 is the line that says $result = ...

 

Here i'm going to be more specific this time

 

I have 1 file called test.php, which has the following:

require_once("includes/functions.php");
require_once("includes/constants.php");
require_once("includes/connection.php");

Among the files in /includes, there is connection.php, which has the following:

<?php
$connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS); 
if (!$connection) {
	die("Database connection failed: " . mysql_error());
}

// 2. Select a database to use 
$db_select = mysql_select_db(DB_NAME,$connection);
if (!$db_select) {
	die("Database selection failed: " . mysql_error());
}
?>

Ok now functions.php has the following (I am now trying a shorter function):

function hasenamebeenregistered($name){
$result = mysql_query("SELECT * FROM 'users'", $connection);
if (!$result) {
	die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
	if($row["name"] == $name){
		return true;
	}
}
return false;
}
//Here i am checking if the name is already in the database. Or atleast trying to..

And lastly, lower down in test.php again, i am calling the function as such:

if(hasenamebeenregistered("example"))
echo "YES";
else
echo "NOOOOOO";

When i run it i get that same ERROR, pointing to the same spot.

I think it is worth noting that my database doesn't have any entries yet, could that be the problem? Thanks!

I added much more information on my setup in my above comment, be editing it. Please read it to understand whats going on better, thanks.

 

And could you explain what you mean by making it a global$ connection? Should that be in the methods.php or connection.php

 

Thanks

Here is your code:

 

function returndataforname($name){

$result = mysql_query("SELECT * FROM 'users'", $connection);

if (!$result) {

die("Database query failed: " . mysql_error());

}

while ($row = mysql_fetch_array($result)) {

if($row["name"] == $name){

$returnvalue['language']=$row['language'];

$returnvalue['name']=$row['name'];

$returnvalue['password']=$row['password'];

return $returnvalue;

}

}

}

 

 

They are telling you to add the $global like below

 

function returndataforname($name){

global $connection;

           

            $result = mysql_query("SELECT * FROM 'users'", $connection);

if (!$result) {

die("Database query failed: " . mysql_error());

}

while ($row = mysql_fetch_array($result)) {

if($row["name"] == $name){

$returnvalue['language']=$row['language'];

$returnvalue['name']=$row['name'];

$returnvalue['password']=$row['password'];

return $returnvalue;

}

}

}

 

This should correct you problem.

Ah thank you WhaleyB and everyone, now i know where to put it!

A) So i understand that it tells the function to look for the variable in the global scale? Is that right?

 

B) Aanyways i ran it again, and i got a different Error. Things are getting better! Now it is telling me that:

 

Database query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users'' at line 1

 

C) I don't have any actual values yet in the table, could that be a problem? If so could you tell me how to do that please.. I haven't got to that part of building my code yet, but if I need it for this to work, then could you tell me quickly how i would do that? Thanks!!

$result = mysql_query("SELECT * FROM 'users'", $connection);

 

 

The above is wrong. Change it to:

 

 

$result = mysql_query("SELECT * FROM users", $connection);

 

Wahoo!! it works!! Thank you so much everyone, i have never been so happy to see firefox say "NOOO!!" lol

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.