Jump to content

Unable to Display Single Row


gamer-goddess

Recommended Posts

==> MySQL version 5.0.91-community

 

So once upon a time I was working on a CMS.

I worked on it for 3 months, had to wipe my computer - but didn't worry about it because I backed up the files on a flash drive... which got smushed by some boxes when I moved <.< awesome -.-

Anyway, a year later (ie: today), I've decided to start again from scratch.

 

For me - for whatever reason - the simplest things always seem to give me the most trouble.

 

I decided to start with a "user" table - naturally - and began working on the first page: user_view.php and added an include file that connects to the database (connectDB.php). Since I started today, everything is simple - no design elements yet - just good 'ol code.

 

The View Users Page is simply a page that grabs all the rows from Table: users and prints the information to the screen, as if you were viewing a Members page on a forum.  This Page works fine. No complaints, does everything I need it to, prints all the rows and cols from Table Users.

 

Then I created the Edit User Page. When I did this a year ago, and I wanted to print out all the data from the table, I used the while loop. When I wanted to only grab certain variables from a table, I just created a new set of variables, as show below. Using the same method from a year ago, I started working on printing various data to the screen (such as "Welcome $username"). 5 minutes of typing... refresh page... nada.

So I stripped away just about every tag but the essentials and it still wouldn't work :shrug:

 

<?php
...
$userID = 3;
$sql = "SELECT * FROM users WHERE userID='$userID'";
$result = mysql_query($sql);
$rows = mysql_fetch_row($result);

echo $rows['username'];
...
?>

 

Normally, this would just grab the username from a row in Table users where the userID = 3

But instead - it does nothing. I just see a blank page. It's a simple 5 lines of code and it's like "im just gonna do w.e the fk i wanna do mmmk thxforplaying"

 

:wtf:

 

PS: Keep in mind there is nothing wrong with the connection to the database, and as already stated I'm able to print ALL values from the Users Table using this same method:

<?php
...
$user_sql = "SELECT * FROM users ORDER BY username ASC";
$user_result = mysql_query($user_sql);
while ( $user_rows = mysql_fetch_array($user_result) )
...
?>

 

 

 

 

Link to comment
Share on other sites

Thanks for the reply, but a solution would be more beneficial

 

Perhaps I'm not explaining myself properly. I've been trying different things.

Previously mysql_fetch_row was written as mysql_fetch_array

Neither work as of October 31 2010

 

One year ago, I distinctly remember typing 3 lines of code to achieve whatever I wanted to achieve.

After googling to find the website that taught me this, I finally found it

http://phpeasystep.com/mysql/6.html

 

From http://phpeasystep.com/mysql/6.html

...

// Select all columns from one row.

"SELECT * FROM table_name WHERE column_name=' value in column '";

 

From http://phpeasystep.com/mysql/6.html

If you don't want looping rows in mysql, replace

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

........

with this

$rows=mysql_fetch_array($result);

 

Using this method, I SHOULD be able to type out $rows['colname'] ANYWHERE I feel like it.

And yet, I'm unable to

 

Keep in mind, I did this one year ago. 3 lines of code. Flawless

Today - nada

What gives?

Link to comment
Share on other sites

 

<?php
$userID = 3;
$sql = "SELECT * FROM users WHERE userID='$userID'";
$result = mysql_query($sql) or die( 'Query: ' . $sql . '<br>Error: ' . mysql_error() );
$rows = mysql_fetch_assoc($result);

echo $rows['username'];
?>

 

 

Moving to PHP help . . .

Link to comment
Share on other sites

<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db   = 'test';

$conn = mysql_connect($host, $user, $pass)
                                                      or die('Failed to connect to mysql');
mysql_select_db($db, $conn);
//also ensure mysql user is attached to correct database with correct privs

$userID = 3;
$sql = "SELECT * FROM users WHERE userID= " . (int)$userID;
$result = mysql_query($sql) or die( 'Query: ' . $sql . '<br>Error: ' . mysql_error() );
$rows = mysql_fetch_assoc($result);

echo '<pre>' . print_r($rows, true) . '</pre>';
//echo $rows['username'];
?>

Link to comment
Share on other sites

nevermind figured it out

 

My original code works fine, and I thought I was going crazy.

 

The problem wasn't with the php, it was with the actual table in the database.

So WITHOUT changing ANY syntax, I deleted the table in the database and re-wrote it with the exact row names as before

and viola~! It worked

 

So something was in fact wrong, but it wasn't with misspelling

I must have set a row type to something weird and didn't realize it, so I just dropped the table altogether

 

CONSIDER THIS PUZZLE SOLVED

;D

 

Link to comment
Share on other sites

You should always have error checking and error reporting logic in your php code so that you will get an indication of when queries fail and you can use msyql_error() to find out why they are failing.

 

Once you check if a query executed without any errors, you should check if a query matched any rows before attempting to fetch any data from those rows.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.