Jump to content

Recommended Posts

I've been working on my first php/mysql page. Obviously, I am having some difficulties with mysql. I've been trying to use mysql_fetch_assoc to grab specific information from a database so that I can display it elsewhere. Here is the code I've been using

 

<?php

// Make a MySQL Connection
mysql_connect("xxx", "xxx", "xxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());

//Getting specific information the database
//database = david_weintraub
//column = planned_hours
//row = $week_1
$p_1_f="SELECT planned_hours FROM david_weintraub WHERE week='$week_1'";
$p_1=mysql_query($p_1_f) or die(mysql_error());
$p_1_row=mysql_fetch_assoc($p_1);
echo $p_1_row['planned_hours'];

?>

 

This doesn't work. It echos nothing back and the page is blank. I'm baffled as to what I am doing wrong. Any help would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/159521-mysql_fetch_assoc-help-for-a-beginner/
Share on other sites

I have a separate page where users submit both planned_hours and week into the database. Does this answer your question? I'm not entirely sure what you mean by 'initialized'.

 

How do I echo out the query to see exactly what's being queried?

You're using '$week_1' in your query, where do you assign it a value?

 

I have a page called david_weintraub.php where $week_1 is assigned a value.

 

echo $p_1_f;

 

When put in my script, this code reads out: SELECT planned_hours FROM david_weintraub WHERE week=''

I have a page called david_weintraub.php where $week_1 is assigned a value.

 

You need to pass it to the page where you're performing the query.  You can use multiple methods, GET, POST, sessions, etc... it all depends on what/how you want to pass it.  Please post some pertinent code from david_weintraub.php where '$week_1' gets its value.

 

When put in my script, this code reads out: SELECT planned_hours FROM david_weintraub WHERE week=''

 

That's exactly my point.  '$week_1' does not have a value, so you're comparing week to nothing which doesn't return anything.

You need to pass it to the page where you're performing the query.  You can use multiple methods, GET, POST, sessions, etc... it all depends on what/how you want to pass it.  Please post some pertinent code from david_weintraub.php where '$week_1' gets its value.

 

Here is the code used in david_weintraub.php where '$week_1' gets its value.

 

$week_1 = 1;
mysql_query("INSERT INTO david_weintraub('week') VALUES ('$week_1');

 

Can you give me an example of how I could pass the information submitted by the david_weintraub.php page to the page I'd like it too (for now that page is called practice.php)?

 

It may help if you see the david_weintraub.php page here:

 

http://barchardslab.site50.net/students/david_weintraub.php

Are you supposed to let the user choose a week?  How are you supposed to know what week it is?

 

The reason I'm asking is because if you're hard-coding $week_1 to '1', then you mine as well just hard code it in the file you perform the query...

The reason I'm asking is because if you're hard-coding $week_1 to '1', then you mine as well just hard code it in the file you perform the query...

 

Okay, I'll do this.

 

I'm still confused as to how to pass information from my david_weintraub.php page to my practice.php. Why can't I access the information submitted into my mysql database through david_weintraub.php when I connect into the same database in both pages. All I want to do with the practice.php is simply display specific information from the database in a customized table. Can you provide me an example of a GET, POST, sessions, etc... method that'd be appropriate for me?

 

Something like this?  A user can enter a number in the next field (week) and it will submit to the same page to check the database.

 

if(isset($_POST['submit']))
{
   $week_1 = $_POST['week'];
   // Make a MySQL Connection
   mysql_connect("xxx", "xxx", "xxx") or die(mysql_error());
   mysql_select_db("xxx") or die(mysql_error());

   //Getting specific information the database
   //database = david_weintraub
   //column = planned_hours  
   //row = $week_1
   $p_1_f="SELECT planned_hours FROM david_weintraub WHERE week='$week_1'";
   $p_1=mysql_query($p_1_f) or die(mysql_error());
   $p_1_row=mysql_fetch_assoc($p_1);
   echo $p_1_row['planned_hours'];
}

?>

</pre>
<form method="POST" action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>">




<

Something like this?  A user can enter a number in the next field (week) and it will submit to the same page to check the database.

 

It is not necessary for a user to enter a week and have that number submit to the same page to check the database.

 

I want to display specific information in the same row as $week_1 into a table. And I would do this for 17 different weeks all in the same table.

You need to use some sort of loop from your query to create the table and insert the values.

 

i.e.

 

</pre>
<table border="1">

   Week
   Planned Hours


for($i=0; $i{ 
?>

   
   hours

} 
?>
</

 

You're also going to need to change your query to select all the planned hours for each week (take out the where condition).

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.