Jump to content

Simple connection to database.


Lee-Bartlett

Recommended Posts

Ok im a novice and all i wanna do i echo back some of my rows in my database so heres the code, i am getting a """"Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\xampp\htdocs\database.php on line 23

database query failed: """""

 

<?php

$db_host = "localhost"; // Your database host server, eg. db.server.com

$db_user = "removed"; // User who has access to the database

$db_pass = "removed"; // User password to access database

$db_name = "removed"; // Existing database name

 

// -- Connecting to the database (not persistent connection)

 

$db = mysql_connect($db_host, $db_user, $db_pass);

mysql_select_db($db_name,$db) or die(mysql_error());

 

 

?>

 

<html>

<head>

 

</head>

<body>

 

<?php

 

$result = mysql_query("SELECT * FROM subjects", $connect); (on this line here)

if (!$result) {

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

}

 

 

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

echo $row[1]." ".$row[2]."<br />";

}

?>

 

</body>

</html>

 

<?php

 

mysql_close($connect);

?>

 

can anyone help me out please

Link to comment
https://forums.phpfreaks.com/topic/123774-simple-connection-to-database/
Share on other sites

supplied argument is not a valid MySQL-Link resource
So did you read the error message and look at your code to find out why the the MySQL-Link resource might not be valid? Where is $connect being set in your code? What variable name are you setting from the mysql_connect() statement?

Ok im a novice and all i wanna do i echo back some of my rows in my database so heres the code, i am getting a """"Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\xampp\htdocs\database.php on line 23

database query failed: """""

 

<?php 
$db_host = "localhost"; // Your database host server, eg. db.server.com
$db_user = "removed"; // User who has access to the database
$db_pass = "removed"; // User password to access database
$db_name = "removed"; // Existing database name

// -- Connecting to the database (not persistent connection)

$db = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name,$db) or die(mysql_error());


?>

<html>
<head>

</head>
<body>

<?php 

$result = mysql_query("SELECT * FROM subjects", $connect); (on this line here)
if (!$result) {
die("database query failed: " . mysql_error());
}


while ($row = mysql_fetch_array($result)) {
echo $row[1]." ".$row[2]."<br />";
}
?>

</body>
</html>

<?php

mysql_close($connect);
?>

 

can anyone help me out please

 

You don't have anything within the variable $connect.  Replace all instances of it with $db, the variable you assigned to with your mysql_connect() function call.

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.