PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old October 8th, 2002, 05:02 PM
alsaffar alsaffar is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 58 alsaffar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Variable inside mysql_query

Hi there,

How I can use a varibale inside mysql_query?

I'm passing a varibale $TableName to a function, and inside the function I make my query depend on the passed variable.

$TableName = 'Users';
TestFunction($TableName);


Function TestFunction($TableName)
{
$Query = " SELECT ID FROM $TableName ";
$Result = mysql_query($Query) or die("Query Failed");
}

How I can get this function works fine?

Reply With Quote
  #2  
Old October 8th, 2002, 05:23 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: Variable inside mysql_query

remove the spaces from the front and end:

$Query = "SELECT ID FROM $TableName";

Reply With Quote
  #3  
Old October 10th, 2002, 12:02 AM
zombie zombie is offline
Codewalkers Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2007
Location: serbia
Posts: 1,876 zombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
RE: Variable inside mysql_query

i am sure that spaces dont matter!

but i think that that code should work. what error do u get? do you even get an error? did u connect with mysql_connect()? did it success?

do you know that mysql_query() doesn't return the value of the 'ID' field, (or any other field) but just a resource id, and u have to use that RID to get value of any field?

(example from php man

php Code:
Original - php Code
  1.  
  2. <?php
  3.    mysql_connect("localhost", "mysql_user", "mysql_password");
  4.    mysql_select_db("mydb");
  5.    $query = "select * from table";
  6.    $result = mysql_query($query);
  7.    while ($row = mysql_fetch_assoc($result)) {
  8.        echo $row["user_id"];
  9.        echo $row["fullname"];
  10.    }
  11.    mysql_free_result($result);
  12. ?>


this is how u get field values from php!

Reply With Quote
  #4  
Old October 11th, 2002, 10:27 AM
crickettdt crickettdt is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Chippenham
Posts: 4 crickettdt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to crickettdt
RE: Variable inside mysql_query

To add:

If you want to fetch a row from the database, use the php function:

mysql_fetch_row

The way to do this is to assign a variable

$row = mysql_fetch_row($query)

then;

echo $row[0];

It then treats the row as an array.

To make it simpler, you can use mysql_fetch_array

then use

echo $row["name of field"];

Allowing you to call the fields by the same names they have in your db.


Reply With Quote
  #5  
Old October 18th, 2002, 04:47 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: Variable inside mysql_query

I am using the code from "Creating Dynamic Content with PHP and MySql" tutorial (see below):

And I get this error from the browser:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in...this line: while($myrow = mysql_fetch_array($result))

Code:

php Code:
Original - php Code
  1.  
  2. <HTML>
  3. <?php
  4. $db = mysql_connect("localhost", "moontots_tester", "tester");
  5. mysql_select_db("moontots_",$db);
  6. $result = mysql_query("SELECT * FROM testdb",$db);
  7. echo "<TABLE>";
  8. echo"<TR><TD><B>Full Name</B><TD><B>Nick Name</B><TD><B>Salary</B></TR>";
  9. while($myrow = mysql_fetch_array($result))
  10. {
  11. echo "<TR><TD>";
  12. echo $myrow["firstname"];
  13. echo " ";
  14. echo $myrow["lastname"];
  15. echo "<TD>";
  16. echo $myrow["nick"]
  17. echo "<TD>";
  18. echo $myrow["salary"];
  19. }
  20. echo "</TABLE>";
  21. ?>
  22. </HTML>   

Reply With Quote
  #6  
Old October 19th, 2002, 12:24 AM
postalcow postalcow is offline
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Ford CIty, PA USA
Posts: 1,267 postalcow User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Send a message via Yahoo to postalcow
RE: Variable inside mysql_query

Anonymous try to post different questions in its own thread. Here try this

php Code:
Original - php Code
  1.  
  2. <html>
  3.  
  4. <?
  5. $db_name = "moontots_";
  6. $tablename = "testdb";
  7. $connection = mysql_connect ("localhost", "moontots_tester", "tester") or die ("Unable to connect");
  8. $db = mysql_selectdb($db_name, $connection) or die ("Unable to Connect to the database");
  9.  
  10. $sql = "SELECT * FROM $tablename";
  11.  
  12. $result = @mysql_query($sql,$connection)
  13.         or die (mysql_error());
  14.  
  15. echo "<TABLE>";
  16. echo"<TR><TD><B>Full Name</B><TD><B>Nick Name</B><TD><B>Salary</B></TR>";
  17.  
  18. while ($myrow = mysql_fetch_array($result)) {
  19.  
  20. $firstname =$row['firstname'];
  21. $lastname =$row['lastname'];
  22. $nick =$row['nick'];
  23. $salary =$row['salary'];
  24.  
  25. }
  26.  
  27. echo "<TR><TD>";
  28. echo "$firstname" ;
  29. echo " ";
  30. echo "$lastname" ;
  31. echo "<TD>";
  32. echo "$nick"
  33. echo "<TD>";
  34. echo "$salary"
  35. echo "</TABLE>";
  36. ?>
  37. </html>

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Variable inside mysql_query


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway