Jump to content

Need Help Resolving The Warning: Mysql_Query(): 4 Is Not A Valid Mysql-Link Resource In


eldan88

Recommended Posts

Hey,

 

It took to couple months to learn PHP and I am trying to extend my knowledge by designing my own ecommerce CMS.

 

I am having difficulties with the building blocks. When I try to run a mySQL query I get the following message:

 

"mysql_query(): 4 is not a valid MySQL-Link resource in"

 

What does this mean and how can I solve this. Btw my connection is successful to the DB.

 

Below is my cose

 

<?php
// Return data from the DB
$query = "SELECT * FROM ecommerce ";
$result = mysql_query($query,$connection);
if(!$result) {
die("mySQL DB read failed" . mysql_error());
}
//use the return data
while($return = mysql_fetch_array($result)) {
$return['category_name'];
}

?>
<?php mysql_close($connection); ?>

 

Any help is apperciated. Thanks!

Where is your connection variable?

 

Hey,

 

Here is my connection variables located

 

<?php require_once("constants.php")// This has all the constants for the DB connection ?>
<?php
// First you make a connection with the database
$connection = mysql_connect(DB, username);
if (!$connection) {
die ( "Could not connect" . mysql_error());
}
// Second you delect which DB you want to work with
// You want to pass along the "handle to mysql_db_select"
$db = mysql_select_db(db_name, $connection);
if (!$db) {
die("Couldn't Select a DB" . mysql_error());
}
mysql_close($connection);

 

 

Below is my constant file

 

<?php
define("DB", "localhost");
define("username", "root");
define("db_name", "ecommerce");
?>

You see where you close the connection? Stop that. Bad coder.

 

Hey, i removed the closed connection on my connection file, and it solved the issue. Thanks a lot for pointing that out!

Also, you're not using a password, for the root user? Seriously?

If, go set a password immediately. Even if it's just your development/test server.

 

Hey. This is just a testing website. It will never be deployed in a publicly, and there isnt any kind of sensitive data. Thats why there isn't any password set.

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.