Azərbaycan dili Bahasa Indonesia Bosanski Català Čeština Dansk Deutsch Eesti English Español Français Galego Hrvatski Italiano Latviešu Lietuvių Magyar Malti Mакедонски Nederlands Norsk Polski Português Português BR Românã Slovenčina Srpski Suomi Svenska Tiếng Việt Türkçe Ελληνικά Български Русский Українська Հայերեն ქართული ენა 中文
Subpage under development, new version coming soon!

Subject: PHP

2007-10-17 17:01:15
luuut [del] to All
I hope somebody will help me. I'm a newbie concerning PHP and I was wondering what I'm doing wrong.

I have this code:
[i]<?
$con = mysql_connect("localhost", "root", "kukkyf78");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db("tekstfjild",$con);

$sql = "SELECT * from tekstfjildtabel";
$tekst = mysql_query($sql,$con);
print_r(mysql_fetch_row($tekst));

mysql_data_seek($tekst,0);
print_r(mysql_fetch_row($tekst));

mysql_close($con);
?>[/i]

It displays like this:
Array ( [0] => Value [1] => ) Array ( [0] => value [1] => )


No I want to display it like this:
Value

What do I have to change?
2007-10-17 17:44:40
[i]
$con = mysql_connect("localhost", "root", "kukkyf78");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db("tekstfjild",$con);

$sql = "SELECT * from tekstfjildtabel";
$tekst = mysql_query($sql,$con);

$result = mysql_fetch_row($tekst);
print($result[0]);


mysql_data_seek($tekst,0);
print_r(mysql_fetch_row($tekst));

mysql_close($con);
[/i]

I've deleted part of your code and added another part. With the line $result = mysql_fetch_row($tekst); you fetch a row and you store it into an array called result. Now, if you want to access a specific element of the array, you just use its index (in the example it prints member 0 of the array). You can even use a loop to print each member.
Hope it helps. [;)]
(edited)
2007-10-17 23:00:46
Thank you. I will take a loot at it, in a moment.
2007-10-17 23:19:28
And you can substitute all this lines:

[i]$con = mysql_connect("localhost", "root", "kukkyf78");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}[/i]

to this:

$con = mysql_connect("localhost", "root", "kukkyf78"); or die('Could not connect: ' . mysql_error());
2007-10-17 23:30:35
I randomly entered this, I have it in another file, so it already links to that file. It's just to show you I didn't do anything wrong there.
2007-10-18 15:35:13
I do it this way

while ($row=mysql_fetch_array(mysql_query($sql))){
print $row;
}


didn't know it could be done in your way, btw.

how would you do a loop with $result array? is there a function which gets number of rows in a table?
2007-10-18 16:13:30
[i]while ($row=mysql_fetch_array(mysql_query($sql))){
print $row;
}[/i]

The code you posted won't do what was requested. It will only print the data type, but not the data. In your example you would probably see Array printed all over the screen.

how would you do a loop with $result array? is there a function which gets number of rows in a table?

If you want to know the size of the array, you can use sizeof() so, following with my example:

$result = mysql_fetch_row($tekst);
$size = sizeof($result);
for ($i=0;$i!=$size;$i++) {
print($result[$i]);
}
(edited)
2007-10-18 16:24:38
Something like this may also work (I am at work now and i can't remember exactly, i will look later)

$result = mysql_fetch_assoc($tekst);
foreach($result as $index=>$content) {
print($content);
}
2007-10-18 16:30:17
Yes, that's definitely another way of doing it. [;)]
Hey, don't work too hard, it might be bad for your health. [:P]
2007-10-18 16:36:59
Well, people that isn't a hippie has to make some money to eat, not all of us has some legumbers in the backyard.

:D
2007-10-18 18:16:56
The code you posted won't do what was requested. It will only print the data type, but not the data. In your example you would probably see Array printed all over the screen.

I know, it was just an example, what whas inside the while wasn't important here :)

thank you for your input anyway.

and, btw, what's the difference between:

[i]while(mysql_fetch_array($request)){...}[/i]

and

[i]while(mysql_fetch_assoc($request)){...}[/i]

?
(edited)
2007-10-18 18:26:11
The difference is that array might return either an associative array or a numeric array.
assoc only returns an associative array.

mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array. ===> from PHP manual
2007-10-30 13:50:49
Up for jeetn.
2007-10-30 14:14:41
Thanks :D
2007-10-30 14:39:28
The thing is luuut... With this script I want to get data out of my database not??

What I want to do is to get data from sokker and put in a database...
2007-10-30 15:10:47
I can't indent the ifs.... Sorry!
Maybe it doesn't fully work, use it as a guide.

$user = "Senhor_Castor";
$password = "password";
// Creates the http object
$http = &new http(HTTP_V11, false);
$http->host = "online.sokker.org";
// saves it on the Session
$_SESSION["http"] = $http;
// array with user and password
$form = array('ilogin' => $user,'ipassword' => $password);
//Posts the request
$code = $http->post("/start.php?session=xml", $form, 'http://'.$http->host);
//If the request went fine
if ($code == HTTP_STATUS_OK) {
//Gets the response and from the response, gets the team Id
$response = $http->get_response_body();
$data = explode("=",$response);
// gets the team ID
$teamId = $data[1];
}

// This for example, returns players.xml using the teamId
function downloadPlayersXML($teamId) {
$http = $_SESSION["http"];
//Sets parameters
$form = array();
//Posts the request
$code = $http->post('/xml/players-'.$teamId.'.xml', $form, 'http://'.$http->host);
if ($code == HTTP_STATUS_OK) {
return $http->get_response_body();
}
else {
return -1;
}
}
(edited)
(edited)