Question

Warning: mysql_num_rows() expects parameter 1 to be resource....

Asked by: seeminglylost

I am creating a simple login page using php and MySQL. I am new at this and cannot figure out why I am getting this error:

Warning: mysql_num_rows() expects parameter 1 to be resource, null given in RegisterUser.php on line 53

Unable to execute the query.

Error code 0:

Code that is being referenced is:
[code]
if (mysql_num_rows($QueryResult) > 0) {
[/code]

I am using xampp by apache friends 1.7.2. I have dumped my database and table several times. Everything appears to be as it should but I don't know where I am going wrong.

My table info is:

SQL result

Host: localhost
Database: songorganizer
Generation Time: Nov 07, 2009 at 01:35 AM
Generated by: phpMyAdmin 3.2.0.1 / MySQL 5.1.37
SQL query: show create table registered_users;
Rows: 1
Table registered_users
Create Table
CREATE TABLE `registered_users` (
`ID` smallint(6) NOT NULL AUTO_INCREMENT,
`email` varchar(40) DEFAULT NULL,
`password` varchar(10) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

Any ideas where it is that I am going wrong??

<?php
session_start();
if (empty($_GET['email']) || empty($_GET['email_confirm'])
	|| empty($_GET['password']) 
	|| empty($_GET['password_confirm']))
	exit("<p>You must enter values in all fields of the Registration form! Click your browser's Back button to return to the previous page.</p>"); 
else if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_GET['email']))
	exit("<p>You must enter a valid e-mail address! Click your browser's Back button to return to the previous page.</p>"); 
else if ($_GET['email'] != $_GET['email_confirm'])
	exit("<p>You did not enter the same e-mail address! Click your browser's Back button to return to the previous page.</p>"); 
else if ($_GET['password'] != $_GET['password_confirm'])
	exit("<p>You did not enter the same password! Click your browser's Back button to return to the previous page.</p>"); 
else if (strlen($_GET['password']) < 5 || strlen($_GET['password']) > 10)
	exit("<p>Your password must be between 5 and 10 characters! Click your browser's Back button to return to the previous page.</p>"); 
$DBConnect = @mysql_connect("localhost", "root")
	Or die("<p>Unable to connect to the database server.</p>"
	. "<p>Error code " . mysql_connect_errno()
	. ": " . mysql_connect_error()) . "</p>"; 
// select and create database
$DBName = "songorganizer";
if (!@mysql_select_db($DBName)) {
	$SQLstring = "CREATE DATABASE $DBName";
	$QueryResult = @mysql_query($SQLstring)
		Or die("<p>Unable to execute the query.</p>"
		. "<p>Error code " . mysql_errno($DBConnect)
		. ": " . mysql_error($DBConnect)) . "</p>";
	echo "<p>You entered your first song!</p>";
	mysql_select_db($DBName);
} 
$TableName = "registered_users";
$SQLstring = "SELECT * FROM $TableName";
$QueryResult = @mysql_query($SQLstring);
if (!$QueryResult) {
 	$SQLstring = "CREATE TABLE $TableName (ID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY, email VARCHAR(40), password VARCHAR(10))";
 	$QueryResult = @mysql_query($SQLstring)
 		Or die("<p>Unable to create the table.</p>"
 		. "<p>Error code " . mysql_errno($DBConnect)
 		. ": " . mysql_error($DBConnect)) . "</p>";
}
$TableName = "registered_users";
$Email = addslashes($_GET['email']);
$Password = addslashes($_GET['password']);
$SQLstring = "SELECT * FROM $TableName";
$QueryResult = @mysql_query($DBConnect, $SQLstring); 
if (mysql_num_rows($QueryResult) > 0) {
	$Row = mysql_fetch_row($QueryResult);
	do {
		if (in_array($Email, $Row))
			exit("<p>Hi, the e-mail address you entered is already
registered! Click your browser's Back button to
return to the previous page.</p>");
		$Row = mysql_fetch_row($QueryResult);
	} while ($Row);
	mysql_free_result($QueryResult);
} 
$SQLstring = "INSERT INTO $TableName VALUES(NULL, '$Email',
'$Password')";
$QueryResult = @mysql_query($DBConnect, $SQLstring)
	Or die("<p>Unable to execute the query.</p>"
 		. "<p>Error code " . mysql_errno($DBConnect)
 		. ": " . mysql_error($DBConnect)) . "</p>";
$_SESSION['ID'] = mysql_insert_id($DBConnect);
mysql_close($DBConnect);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The Song Organizer Site</title><img src="Butterfly/images/header.gif" width="875" height="50" alt="" title="" border="0" align="right" />
<link rel="stylesheet" href="php_styles.css" type="text/css" />
<meta http-equiv="content-type"
      content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Song Organizer</h1>
<h2>User Registration</h2>
<p>Your new user ID is <strong>
<?= $_SESSION['userID'] ?></strong>.</p>
<p><a href="UpdateContactInfo.php">Enter Contact Information</a></p>
</body>
</html>

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-11-06 at 21:42:13ID24879998
Topics

MySQL Server

,

PHP Scripting Language

Participating Experts
4
Points
500
Comments
12

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. phpmyadmin and mysql simple question
    I have all my phpmyadmin setup and mysql server is running. My website is under apache server. I got the program off the web which is easyphp and it install apache , php and mysql for you and it runs without changing any configrations. my all website files are under the d...
  2. MYSQL & Apache Problem
    I'm not sure what the problem is, but i think it is with apache. Here is my situation. I have an offsite webhost who is running php/mysql/apache, etc. So, everything there is working correctly. We are in the process of bringing the website in-house, so i'm setting up a we...
  3. phpMyAdmin and MySQL
    Hi ee Having converted Access to MySQL, how do i then upload it onto the server (on internet)- can i do this through phpMyAdmin? Seb

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: seetharamlPosted on 2009-11-06 at 21:54:56ID: 25765154

Hi seeminglylost,

can you remove
@ symbol before mysql_quey and let me know what error you get after remove @.


Thanks

 

by: aisebastianPosted on 2009-11-06 at 23:24:21ID: 25765300

Hi,

You could try to alter line 43 to this:

$SQLstring = "SELECT * FROM `$TableName`";

 

by: ChrisStanyonPosted on 2009-11-07 at 06:39:33ID: 25766459

Try writing lines 45-55 like this:

while ($Row = mysql_fetch_row($QueryResult))
{
	if (in_array($Email, $Row))
		exit("<p>Hi, the e-mail address you entered is already registered! Click your browser's Back button to return to the previous page.</p>");
}

                                              
1:
2:
3:
4:
5:

Select allOpen in new window

 

by: Ray_PaseurPosted on 2009-11-07 at 07:21:40ID: 25766567

This thing:

Warning: mysql_num_rows() expects parameter 1 to be resource, null given

... means that your query failed.  MySQL is not a "black box" and you have to test for query success or failure before you try to use the query resources identifier or the contents of the query results set.

This code snippet shows the correct way to construct and run a query, how to test for success, and how to find out what went wrong if the query failed (if there was a syntax error, if the DB server flipped, etc.)  Please read it over carefully - code, comments and the manual page references - and post back here with any questions.  I am sure we can get you moving in the right direction ;-)

Best regards, ~Ray

<?php // RAY_mysql_example.php
error_reporting(E_ALL); 
// IMPORTANT PAGES FROM THE MANUALS
// MAN PAGE: http://us2.php.net/manual/en/ref.mysql.php
// MAN PAGE: http://us2.php.net/manual/en/mysql.installation.php
// MAN PAGE: http://us.php.net/manual/en/function.mysql-error.php 

// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??"; 

// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-connect.php
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
   $errmsg = mysql_errno() . ' ' . mysql_error();
   echo "<br/>NO DB CONNECTION: ";
   echo "<br/> $errmsg <br/>";
} 
// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
   $errmsg = mysql_errno() . ' ' . mysql_error();
   echo "<br/>NO DB SELECTION: ";
   echo "<br/> $errmsg <br/>";
   die('NO DATA BASE');
}
// IF WE GOT THIS FAR WE CAN DO QUERIES 

// ESCAPING A DATA FIELD FOR USE IN MYSQL QUERIES
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-real-escape-string.php
$safe_username = mysql_real_escape_string($_POST["username"]); 

// CREATING AND SENDING A SELECT QUERY AND TESTING THE RESULTS
// MAN PAGE:http://us2.php.net/manual/en/function.mysql-query.php
$sql = "SELECT id FROM my_table WHERE username='$safe_username'";
$res = mysql_query($sql); 
// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
// MAN PAGE: http://us.php.net/manual/en/function.mysql-error.php
if (!$res)
{
   $errmsg = mysql_errno() . ' ' . mysql_error();
   echo "<br/>QUERY FAIL: ";
   echo "<br/>$sql <br/>";
   die($errmsg);
}
// IF WE GET THIS FAR, THE QUERY SUCCEEDED AND WE HAVE A RESOURCE-ID IN $res SO WE CAN NOW USE $res IN OTHER MYSQL FUNCTIONS 

// DETERMINE HOW MANY ROWS OF RESULTS WE GOT
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-num-rows.php
$num = mysql_num_rows($res);
if (!$num)
{
   echo "<br/>QUERY FOUND NO DATA: ";
   echo "<br/>$sql <br/>";
}
else
{
   echo "<br/>QUERY FOUND $num ROWS OF DATA ";
   echo "<br/>$sql <br/>";
} 

// ITERATE OVER THE RESULTS SET TO SHOW WHAT WE FOUND
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-fetch-assoc.php
echo "<pre>\n"; // MAKE IT EASY TO READ
while ($row = mysql_fetch_assoc($res))
{
   var_dump($row); // MAN PAGE: http://us2.php.net/manual/en/function.var-dump.php
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:

Select allOpen in new window

 

by: seeminglylostPosted on 2009-11-07 at 14:15:29ID: 25768303

I want to thank everyone for their responses.  With the code snippet that Ray_Paseur I was able to resolve my issues.

The problem that I am having now is that my script is not displaying the information from the MySQL database.

On this particular page that I have included the code snippet for, it should display the userID and it does not.

Any suggestions?

My table info for userID is as follows:

Field        Type                Null        
userID       smallint(6)       No

and it is set to auto_increment.

<?php
session_start();
if (empty($_GET['email']) || empty($_GET['email_confirm'])
	|| empty($_GET['password']) 
	|| empty($_GET['password_confirm']))
	exit("<p>You must enter values in all fields of the Registration form! Click your browser's Back button to return to the previous page.</p>"); 
else if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_GET['email']))
	exit("<p>You must enter a valid e-mail address! Click your browser's Back button to return to the previous page.</p>"); 
else if ($_GET['email'] != $_GET['email_confirm'])
	exit("<p>You did not enter the same e-mail address! Click your browser's Back button to return to the previous page.</p>"); 
else if ($_GET['password'] != $_GET['password_confirm'])
	exit("<p>You did not enter the same password! Click your browser's Back button to return to the previous page.</p>"); 
else if (strlen($_GET['password']) < 5 || strlen($_GET['password']) > 10)
	exit("<p>Your password must be between 5 and 10 characters! Click your browser's Back button to return to the previous page.</p>"); 
$DBConnect = @mysqli_connect("localhost", "root")
	Or die("<p>Unable to connect to the database server.</p>"
	. "<p>Error code " . mysqli_connect_errno()
	. ": " . mysqli_connect_error()) . "</p>"; 
$DBName = "songorganizer"; 
@mysqli_select_db($DBConnect, $DBName)
	Or die("<p>Unable to select the database.</p>"
	. "<p>Error code " . mysqli_errno($DBConnect)
	. ": " . mysqli_error($DBConnect)) . "</p>"; 
$TableName = "users";
$Email = addslashes($_GET['email']);
$Password = addslashes($_GET['password']);
$SQLstring = "SELECT * FROM $TableName";
$QueryResult = @mysqli_query($DBConnect, $SQLstring); 
if (mysqli_num_rows($QueryResult) > 0) {
	$Row = mysqli_fetch_row($QueryResult);
	do {
		if (in_array($Email, $Row))
			exit("<p>The e-mail address you entered is already
registered! Click your browser's Back button to
return to the previous page.</p>");
		$Row = mysqli_fetch_row($QueryResult);
	} while ($Row);
	mysqli_free_result($QueryResult);
} 
$SQLstring = "INSERT INTO $TableName VALUES(NULL, '$Email',
'$Password')";
$QueryResult = @mysqli_query($DBConnect, $SQLstring)
	Or die("<p>Unable to execute the query.</p>"
 		. "<p>Error code " . mysqli_errno($DBConnect)
 		. ": " . mysqli_error($DBConnect)) . "</p>";
$_SESSION['userID'] = mysqli_insert_id($DBConnect);
mysqli_close($DBConnect);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The Song Organizer Site</title>
<link rel="stylesheet" href="php_styles.css" type="text/css" />
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Song Organizer</h1>
<h2>User Registration</h2>
<p>Your new user ID is <strong><?= $_SESSION['userID'] ?></strong>.</p>
<p><a href="SongOrganizer.php">User Home Page</a></p>
</body>
</html>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:

Select allOpen in new window

 

by: Ray_PaseurPosted on 2009-11-07 at 14:26:22ID: 25768348

Let's start with some basic debugging techniques.  First, place this statement right at the top before session_start()

error_reporting(E_ALL);

Next find all those @-signs on the function statements and remove them.  The @ suppresses errors, and when you don't know why your script does not work, you very much want to see the errors.  

Add in the tests for query success, like shown in lines 41 - 50 of the snippet I posted above.  Add this sort of test everywhere you run a query.  If you're unsure how to do this, it's OK to refer to the  manual pages - there are some differences between mysql and mysqli, but the principles remain the same.  You MUST test to see if the query succeeded before you can do anything with the resource or results set.

Please make those changes, work out the parse errors, and run it again then repost the new code, along with any messages that you received when you tried to run the new code.  Thanks, ~Ray

 

by: seeminglylostPosted on 2009-11-07 at 15:19:57ID: 25768558

Ok, I did as you suggested.  All of the @ were removed.  Placed this statement right at the top before session_start()

error_reporting(E_ALL).

I added in the test for query success.  The only error message that came up was:

Deprecated: Function eregi () is deprecated in...

I went through and changed my code accordingly.  Ran it.  No other errors are being displayed.  Page is working fine, except that it is not displaying the userID.

I have added a print screen for the output.  The output should be displaying the user ID number and it does not.  This is what has me stumped.

Revised code is attached with query tests and @ removed

<?php
error_reporting(E_ALL);
session_start();
if (empty($_GET['email']) || empty($_GET['email_confirm'])
	|| empty($_GET['password']) 
	|| empty($_GET['password_confirm']))
	exit("<p>You must enter values in all fields of the Registration form! Click your browser's Back button to return to the previous page.</p>"); 
else if (!preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^", $_GET['email']))
	exit("<p>You must enter a valid e-mail address! Click your browser's Back button to return to the previous page.</p>"); 
else if ($_GET['email'] != $_GET['email_confirm'])
	exit("<p>You did not enter the same e-mail address! Click your browser's Back button to return to the previous page.</p>"); 
else if ($_GET['password'] != $_GET['password_confirm'])
	exit("<p>You did not enter the same password! Click your browser's Back button to return to the previous page.</p>"); 
else if (strlen($_GET['password']) < 5 || strlen($_GET['password']) > 10)
	exit("<p>Your password must be between 5 and 10 characters! Click your browser's Back button to return to the previous page.</p>"); 
$DBConnect = mysqli_connect("localhost", "root")
	Or die("<p>Unable to connect to the database server.</p>"
	. "<p>Error code " . mysqli_connect_errno()
	. ": " . mysqli_connect_error()) . "</p>"; 
$DBName = "songorganizer"; 
mysqli_select_db($DBConnect, $DBName)
	Or die("<p>Unable to select the database.</p>"
	. "<p>Error code " . mysqli_errno($DBConnect)
	. ": " . mysqli_error($DBConnect)) . "</p>"; 
$TableName = "users";
$Email = addslashes($_GET['email']);
$Password = addslashes($_GET['password']);
$SQLstring = "SELECT * FROM $TableName";
$QueryResult = mysqli_query($DBConnect, $SQLstring); 
if (mysqli_num_rows($QueryResult) > 0) {
	$Row = mysqli_fetch_row($QueryResult);
	do {
		if (in_array($Email, $Row))
			exit("<p>The e-mail address you entered is already
registered! Click your browser's Back button to
return to the previous page.</p>");
		$Row = mysqli_fetch_row($QueryResult);
	} while ($Row);
	mysqli_free_result($QueryResult);
} 
$SQLstring = "INSERT INTO $TableName VALUES(NULL, '$Email',
'$Password')";
$QueryResult = mysqli_query($DBConnect, $SQLstring)
	Or die("<p>Unable to execute the query.</p>"
 		. "<p>Error code " . mysqli_errno($DBConnect)
 		. ": " . mysqli_error($DBConnect)) . "</p>";
$_SESSION['userID'] = mysqli_insert_id($DBConnect);
mysqli_close($DBConnect);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The Song Organizer Site</title>
<link rel="stylesheet" href="php_styles.css" type="text/css" />
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Song Organizer</h1>
<h2>User Registration</h2>
<p>Your new user ID is <strong><?= $_SESSION['userID'] ?></strong>.</p>
<p><a href="SongOrganizer.php">User Home Page</a></p>
</body>
</html>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:

Select allOpen in new window

 

by: Ray_PaseurPosted on 2009-11-07 at 15:50:19ID: 25768677

Great - that is progress.  Now we want to add logic and data visualization to the script.  Right before executing each query, add an echo statement to print out the contents of the query.  For example, between lines 28 and 29, print out the variable $SQLstring so we can see what is being passed to the query function.  Do this for each of the queries.  It will help us to see what is getting created in the query strings and will help us follow the program logic flow through the code.

Finally, please change line 62 as follows (you may have short-tags turned off):

<p>Your new user ID is <strong><?php echo "{$_SESSION['userID']}"; ?></strong>.</p>

                                              
1:

Select allOpen in new window

 

by: seeminglylostPosted on 2009-11-07 at 17:31:18ID: 25768975

Ok...everything now works.

I do have one question.  If I change everything that has mysqli to mysql, it cannot connect to the database.  If I change it back to mysql, it works.  Can you explain to me why this is?

 

by: Ray_PaseurPosted on 2009-11-07 at 18:55:00ID: 25769237

Sort of - mysqli and mysql do many of the same things, but use different syntax, sometimes with operands in different order.  And mysqli is mostly intended to be object-oriented. That is probably your issue.  If you are curious, you can look the functions up on the PHP.net web site.

http://us.php.net/manual/en/function.mysql-connect.php
http://us.php.net/manual/en/mysqli.connect.php

Best regards, ~Ray

 

by: seeminglylostPosted on 2009-11-07 at 20:37:59ID: 31651322

Thanks so very much Ray!  I have bookmarked the links you provided for future reference!

 

by: Ray_PaseurPosted on 2009-11-08 at 06:18:04ID: 25770545

Thanks for the points - a good question, for sure! ~Ray

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...