Jump to content

Basic PHP/MySQL Code problem


ZandarGlass

Recommended Posts

My apologies if I've placed this in the wrong section. It concerns a basic connection to n SQL database, and doing a simple count of the records.

I'm learning from DAVID POWERS PHPSOLOUTIONS book. Examples in his book show connecting to the local db differently than my host wants me to connect, and I believe therin lies the issue. If I can figure out the method or problem I'm having, I'll be very thankful.
 

I'm just connecting to a database, running one query, and counting records. The code that I altered, and I am trying to use, looks like this:
 

 

<?php
//Sample Database Connection Syntax for PHP and MySQL.
include ('/includes/imageconn.inc.php');
include ('/includes/imagetableconn.inc.php');

mysql_select_db($dbname);

# Check If Record Exists

$sql = "SELECT * FROM $usertable";

$result = $conn->query($sql) or die(mysqli_error());

$numrows = $result->num_rows;


?>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo $numrows; ?>
</body>
</html>

-----------------------------------------------------------------------

 

My imageconn.inc.php file holds a set of variables needed for the connection. I have verified it works (online, where I want), using other simple pages I made.

 

This is the imagetableconn.inc.php file. It contains one line of code (which also works in the test file the host sent me).

 

$conn = mysql_connect($hostname, $username, $password) or die ('Error connecting to mysql');

 

-------------------------------------------------------------------------

 

I'm pretty sure the problem lies in this line (which is from the BOOK, which I can't figure out how to modify for my online needs):
 

$result = $conn->query($sql) or die(mysqli_error());

 

 

My error message: Fatal error: Call to a member function query() on a non-object in D:\Hosting\4641474\html\testimagedb1.php on line 12
 

Link to comment
https://forums.phpfreaks.com/topic/283508-basic-phpmysql-code-problem/
Share on other sites

You're mixing procedural code with object oriented code!

You're also mixing mysql_* with mysqli_*

 

Try to connect with this!

$conn =  new mysqli($hostname, $username, $password, $dbname);
// check if connection attempt returned an error number
if(mysqli_connect_errno()){
     echo 'Error connecting to database!';
     exit; # kill script; couldn't connect to database
}

Delete this because we're are not using msyql_*   we're using mysqli, which is what you want to use! 

mysql_select_db($dbname);

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.