/    Sign up×
Community /Pin to ProfileBookmark

Why isnt this code working?

I am trying have the following code access a MySQL database created in phpMyAdmin. It keeps getting stuck on the second paragraph where it says

“There are currently no stores in our database”

The proper table is implemented in the code. I am lost. Any help would be much appreciated. Thanks

[QUOTE]

<?
$username=”albanygr”;
$password=”password”;
$database=”albanygr_DBTest”;

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);

$query=”SELECT * FROM timetest”;
$result=mysql_query($query);
$num=mysql_numrows($result) or die(‘There are currently no stores in our database.’);

echo ‘<table border=”0″ cellspacing=”2″ cellpadding=”2″>
<tr>
<th><font face=”Arial, Helvetica, sans-serif”>Store Name</font></th>
<th><font face=”Arial, Helvetica, sans-serif”>Hours of Operation</font></th>’;

$i=0;

while ($i < $num) {
$name=mysql_result($result,$i,”store_name”);
$open=mysql_result($result,$i,”open_time”);
$close=mysql_result($result,$i,”close_time”);

echo ‘<tr>
<td><font face=”Arial, Helvetica, sans-serif”>’.$name.'</font></td>
<td><font face=”Arial, Helvetica, sans-serif”>’.$open.’ – ‘.$close.'</font></td></tr>’;
$i++;
}

echo ‘</table>’;

mysql_close();
?>

[/QUOTE]

to post a comment
PHP

24 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Jan 12.2007 — [code=php]$num=mysql_num_rows($result) or die('There are currently no stores in our database.');[/code]
Copy linkTweet thisAlerts:
@MichaelttkkJan 12.2007 — hey sac8513,

Change this
[code=php]
<?
$username="albsdfasdr";
$password="pasfdsa";
$database="sdfadf";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM sdfsfadst";
[/code]


to
[code=php]
<?
$username="user";
$password="password";
$database="databasename";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM tablename";
[/code]


I think it is better for good
Copy linkTweet thisAlerts:
@sac8513authorJan 12.2007 — As I mentioned i have changed the tablename to the appropriate table. Still not working.

Is there something that could be wrong with my SQL table?
Copy linkTweet thisAlerts:
@sac8513authorJan 12.2007 — NIGHTSHIFT, Thanks i did not see that typo before. For some reason though it still did no change the result on the website.

It still goes to "There are currently no stores in database"

Could there be something wrong with my database that it is not recognizing the rows? If so, what could i do to correct this?
Copy linkTweet thisAlerts:
@NogDogJan 12.2007 — Let's maximize our bug detection:
[code=php]
$username="albanygr";
$password="password";
$database="albanygr_DBTest";

$dblink = mysql_connect(localhost,$username,$password) or die("DB connection failed");
@mysql_select_db($database) or die( "Unable to select database<br />".mysql_error());

$query="SELECT * FROM timetest";
$result=mysql_query($query) or die("$query <br />".mysql_error());
$num=mysql_numrows($result) or die('There are currently no stores in our database.');
[/code]
Copy linkTweet thisAlerts:
@NightShift58Jan 12.2007 — [code=php]$num=mysql_numrows($result) or die('There are currently no stores in our database.');[/code][/QUOTE]The above will work but is deprecated - so you don't know how long it will be working. Use [b]mysql_num_rows()[/b] instead.

I meant to have written that but don't know where the comments went...
Copy linkTweet thisAlerts:
@sac8513authorJan 12.2007 — NightShift, Yes i realized this. Even with the change it still did not work.

In my response to that before I asked if there could be a problem with my database not recognizing the rows. If that was the case what could i do to correct this?

Currently i dont have anything indexed (if that is even necessary) and in within phpmyadmin it shows rows =0 under row statistics box. Could this have anything to do with this?

Thanks again
Copy linkTweet thisAlerts:
@NightShift58Jan 12.2007 — What you're getting is not an error message but an application message.

You're only telling the user that you didn't find any stores. It's not really a reason to die(), but that's another issue...

Have you made the changes recommended by NogDog? You should...

If and when you do and you still get the same results - meaning the script is not die()ing earlier - then you probably don't have any stores in this table, in this database. Have you looked with phpMyAdmin? Are there records in the table?
Copy linkTweet thisAlerts:
@sac8513authorJan 12.2007 — Yes I have updated the code provided by NogDog. Still it is telling me that there are currently no stores in the database.

I have set up a table within the databse to test this out. There are 3 fields: One is the name of the store, the opening time , and closing time.

What I am trying to accomplish as you can see from the code is to establish to the user whether or not the particular store is still open or if it is closed. But because the code stops running at that point i do not know what could be wrong.

Thanks
Copy linkTweet thisAlerts:
@NightShift58Jan 12.2007 — [code=php]<?
include "DBconnect.inc.php";

$query="SELECT * FROM timetest";
$result=mysql_query($query) or die("$query <br />".mysql_error());

echo '<table border="0" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Store Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Hours of Operation</font></th>
</tr>';

$i=0;

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

$name = $row['store_name'];
$open = $row['open_time'];
$close = $row['close_time'];

echo "<tr>
<td><font face='Arial, Helvetica, sans-serif'>$name</font></td>
<td><font face='Arial, Helvetica, sans-serif'>$open - $close</font></td></tr>
</tr>";
}

echo '</table>';

mysql_close();
?>[/code]
Dbconnect.inc.php contains the equivalent of:[code=php]$username="albanygr";
$password="password";
$database="albanygr_DBTest";

$dblink = mysql_connect(localhost,$username,$password) or die("DB connection failed");
@mysql_select_db($database) or die( "Unable to select database<br />".mysql_error()); [/code]
You can see the posted code at: http://www.nightshift58.com/webdev/timetest.phpI have no way of testing it right now, but I'm not sure that just writing [b]localhost[/b] will do. You may need to put quotes around that.
Copy linkTweet thisAlerts:
@sac8513authorJan 12.2007 — Thank you . I am now finally having something show on my website.

Unfortunately it does not seem to be reading any information in my table. All I see are the headers "Store Name" and 'Hours of Opertation". Nothing is showing up underneath.

What am I doing wrong within phpMyAdmin to be causing this. I included a screenshot attatchment below to show what my table looks like. Thanks

[upl-file uuid=5220aaf2-5533-47c6-8e4d-0ef37bcd70f4 size=16kB]table.GIF[/upl-file]
Copy linkTweet thisAlerts:
@NightShift58Jan 12.2007 — For one, the field name "store_name" doesn't exist, What's there is a field called "paesans".

Second, even if the field name was correct, the field is only 1 character long. Hardly enough room to save a store name.

Third, the "Browse" tab in phpMyAdmin is inactive. That usually means that the table is empty.

So, you need to change the script to use the field name now in the table or change the field name in the table to the one used in your script.

And... you need to put some data in there...
Copy linkTweet thisAlerts:
@sac8513authorJan 12.2007 — ok i see i see. But then where would i place "paesans"? In the default column?

I appreciate your patience. I am very new to this
Copy linkTweet thisAlerts:
@NightShift58Jan 12.2007 — Either you change the field name from "paesans" to "store_name" (and "paesans" disappears forever) OR you change your script and put "paesans" where "store_name" now is (and "store_name" disappears forever).
Copy linkTweet thisAlerts:
@sac8513authorJan 12.2007 — Ok fine so I changed the field to "store_name" but now what I am saying is where are these "store_names" now going to appear from in order for the information about each store to be seen?
Copy linkTweet thisAlerts:
@NightShift58Jan 12.2007 — You also need to change the length of the field "store_name" (with phpMyAdmin). Use whatever length you think will cover your needs. Use "varchar" instead of "char". If you're not sure how long to go, use 255 for now.

Next question is: Where is your data coming from?
Copy linkTweet thisAlerts:
@sac8513authorJan 12.2007 — Yes I changed the appropriate length of the field as well.

Well as you mentioned before it seems that the table is empty. So i guess I would have to ask you how do I fill the table?
Copy linkTweet thisAlerts:
@NightShift58Jan 12.2007 — If you don't have any data, you can use phpMyAdmin to enter data. Later, when you know how to disgn form and do table inserts, you'll be able to design your own input forms but for now... phpMyAdmin is the place you can enter 5-6 stores to test your script.
Copy linkTweet thisAlerts:
@sac8513authorJan 12.2007 — I dont think you noticed but I changed my post from above to ask you how I fill the table.

You mentioned that the browse function was inactive which I notice now as well. Therefore I do not know how to enter that info.

Once i enter it will it then show on my website under the columns we created?
Copy linkTweet thisAlerts:
@NightShift58Jan 12.2007 — You can do that in phpMyAdmin.
Copy linkTweet thisAlerts:
@sac8513authorJan 12.2007 — A -ha. I got it! Thank you.


Why doesnt this information now show on my webpage after I got that code to work from before?
Copy linkTweet thisAlerts:
@NightShift58Jan 12.2007 — What did you enter?
Copy linkTweet thisAlerts:
@sac8513authorJan 12.2007 — Nevermind I got it. Amazing. Thanks a lot.
Copy linkTweet thisAlerts:
@NightShift58Jan 12.2007 — You're welcome.... and enjoy it...

No other script will ever give you this much pleasure...?
×

Success!

Help @sac8513 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.9,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...