[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 09-12-2005, 03:32 PM   #1 (permalink)
NamePros Regular
 
buddybuddha's Avatar
 
Join Date: May 2005
Location: PA
Posts: 212
74.00 NP$ (Donate)

buddybuddha is an unknown quantity at this point


30 NP$ For Whoever Can Give Me Simple Code Snippet

Im looking for a small sql code snippet to do an extremely simple task. Surely I could read an online tutorial, but Im really lazy. So there a 30 NP$ reward for whoever can give me the code.

I know how to connect to the database and how to select a database.

So assuming Im in the database, how do I:

1- Select a table named plog_users
2- View a given row, lets say for your code, row #2
3- Store the data contained in that raw as variable $nug
buddybuddha is offline  
Old 09-12-2005, 03:41 PM   #2 (permalink)
SQLdumpster.com
 
miseria's Avatar
 
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 545
205.50 NP$ (Donate)

miseria will become famous soon enoughmiseria will become famous soon enough


PHP Code:
<?php

$db_host
= "localhost";
$db_username = "";
$db_password = "";
$database = "plog_users";


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


$query = "SELECT * FROM 'row#2' ";
$result=mysql_query($query);
$num=mysql_num_rows($result);

$i=0;
while (
$i < $num) {

$nug=mysql_result($result,$i,"row#2");

$i++;
}


?>

I think something along those lines
miseria is offline  
Old 09-12-2005, 03:53 PM   #3 (permalink)
NamePros Regular
 
buddybuddha's Avatar
 
Join Date: May 2005
Location: PA
Posts: 212
74.00 NP$ (Donate)

buddybuddha is an unknown quantity at this point


I think thats wrong because the database isn't plog_users the table is.
buddybuddha is offline  
Old 09-12-2005, 03:57 PM   #4 (permalink)
SQLdumpster.com
 
miseria's Avatar
 
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 545
205.50 NP$ (Donate)

miseria will become famous soon enoughmiseria will become famous soon enough


PHP Code:
<?php

$db_host
= "localhost";
$db_username = "";
$db_password = "";
$database = "";


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


$query = "SELECT 'row#2' FROM 'plog_users' WHERE UID=' ' ";
$result=mysql_query($query);
$num=mysql_num_rows($result);

$i=0;
while (
$i < $num) {

$nug=mysql_result($result,$i,"row#2");

$i++;
}


?>
I'm sure somebody will corect me on that. I have my own frustrations with MySQL

Last edited by miseria; 09-12-2005 at 04:09 PM.
miseria is offline  
Old 09-12-2005, 04:01 PM   #5 (permalink)
NamePros Regular
 
buddybuddha's Avatar
 
Join Date: May 2005
Location: PA
Posts: 212
74.00 NP$ (Donate)

buddybuddha is an unknown quantity at this point


Ah wait I forgot something too. Im looking for a specific column as well. The column name is UID.
buddybuddha is offline  
Old 09-12-2005, 04:09 PM   #6 (permalink)
SQLdumpster.com
 
miseria's Avatar
 
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 545
205.50 NP$ (Donate)

miseria will become famous soon enoughmiseria will become famous soon enough


Edited ^

It's wrong but it's a guideline

Last edited by miseria; 09-12-2005 at 04:13 PM.
miseria is offline  
Old 09-12-2005, 04:13 PM   #7 (permalink)
NPQ's PA, Slave, and On Call Coder

Technical Services


 
Eric's Avatar
 
Join Date: Mar 2005
Posts: 4,545
0.71 NP$ (Donate)

Eric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond repute

Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse
Something like this?

PHP Code:
<?php

$dbhost
= "localhost";
$dbuser = "username";
$dbpass = "password";
$dbname = "";


$db = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $db) or die("Error: " .mysql_error());


$query = mysql_query("SELECT `UID` FROM `plog_users`") or die("Error: " .mysql_error());
$result = mysql_query($query);

while (
$row = mysql_fetch_array($result))
{
$nug = $row['row#2'];
echo
$nug;
}


?>
__________________
Eric is offline  
Old 09-12-2005, 04:36 PM   #8 (permalink)
NamePros Member
 
Join Date: Jul 2005
Posts: 100
45.15 NP$ (Donate)

Le Wolf is on a distinguished roadLe Wolf is on a distinguished road


PHP Code:
$result = mysql_query("SELECT `UID` FROM `plog_users`");
$uid = mysql_result($result,1); // row 2
__________________
My host is better than yours
Le Wolf is offline  
Old 09-13-2005, 01:00 PM   #9 (permalink)
NamePros Regular
 
buddybuddha's Avatar
 
Join Date: May 2005
Location: PA
Posts: 212
74.00 NP$ (Donate)

buddybuddha is an unknown quantity at this point


Hmm well that just about did it, sorta. And since Iused part of everyones code, I couldnt think of a fair way to distribute the NP$.

So whoever can figure out this error gets the 30 + a bonus 10!

I wrote the following code

Code:
<?php

$x = 0;

/* Set mysql variables */
$db_host = "localhost"; //Leave localhost
$db_user = "buddha_nugget"; //DB username
$db_name = "buddha_nugget"; //DB Name
$db_pass = "*****"; //DB User password

$connect = mysql_connect("$db_host","$db_user","$db_pass") or die(mysql_error());
$select_db = mysql_select_db("$db_name");

//Do sqlquery and get variable num
$sqlquery = mysql_query("SELECT * FROM plog_users");
$num = mysql_num_rows($sqlquery);

echo $num;
echo "<br>";

//Found Number of Rows

while ($x == 0)
{

	//Generate Random Number
	$random = rand(0,$num-1);

	//Connect
	$dbhost = "localhost";
	$dbuser = "buddha_nugget";
	$dbpass = "*****";
	$dbname = "buddha_nugget";

	$db = mysql_connect($dbhost, $dbuser, $dbpass);
	mysql_select_db($dbname, $db) or die("Error: " .mysql_error());

	$query = mysql_query('SELECT id FROM plog_users');
	$uid = mysql_result($query, $random); //Random Row

	$result = mysql_query('SELECT status FROM plog_users');
	$stat = mysql_result($result, $random); //Random Row

	if ($stat == "1"){$x = 1;}
	
	echo $random;
	echo "<br>";
	echo $uid;
	echo "<br>";
	echo $stat;
	echo "<br>";
}

echo "final";
echo "<br>";
echo $random;
echo "<br>";
echo $uid;
echo "<br>";
echo $stat;

?>
Tried to comment, but its a diagnostics code to tell me why it isnt working.

Inside the plog_users table I have the following data.

id column- 1,22,21
status column- 1,3,2

After running the diagnostic program I get wierd results.

something along the lines of

3
2
22
2
1
21
3
0
1
1
final
0
1
1

The 3 is right, there are that man rows. Also, the 0,1,1 part is right.

But grouping the other two

2,22,2
and
1,21,3
which represent row,id,status

we find an error.

Row 2 is supposed to have 21,2
Row 1 is supposed to have 22,3

So it finds the proper status code, but not the proper id.

Anyone know why?

BTW- Im using phpmyadmin to view the tables.

Also, the grouping of those errors is consistent.
buddybuddha is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial: Creating a simple cgi hit counter for your site using Perl deadserious Webmaster Tutorials 3 05-29-2004 01:31 AM
A simple cgi hit counter for your site using Perl deadserious CODE 2 11-07-2003 08:23 AM

Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 03:36 PM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

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 82 83 84 85