Jump to content


Photo

Basic PHP/MySQL Code problem

sql php noob object phpsolutions

  • Please log in to reply
2 replies to this topic

#1 ZandarGlass

ZandarGlass

    Member

  • Members
  • PipPip
  • 12 posts

Posted 01 November 2013 - 11:05 AM

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
 



#2 objnoob

objnoob

    Advanced Member

  • Members
  • PipPipPip
  • 332 posts

Posted 01 November 2013 - 11:17 AM

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);

Edited by objnoob, 01 November 2013 - 11:21 AM.


#3 ZandarGlass

ZandarGlass

    Member

  • Members
  • PipPip
  • 12 posts

Posted 01 November 2013 - 12:08 PM

Ah. Got it. Thanks!
 


Edited by ZandarGlass, 01 November 2013 - 12:09 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com