Jump to content

mysql api in php


mrbean

Recommended Posts

in php you have the following mysql API fetch commands:

■mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both

■mysql_fetch_assoc — Fetch a result row as an associative array

■mysql_fetch_field — Get column information from a result and return as an object

■mysql_fetch_lengths — Get the length of each output in a result

■mysql_fetch_object — Fetch a result row as an object

■mysql_fetch_row — Get a result row as an enumerated array

 

If u use one of them for example i use mysql_fetch_object.

$run = mysql_query(SELECT `status` FROM `[settings]` WHERE `name`= *);
$display = mysql_fetch_object($run); 

other example

$run = mysql_query(SELECT `status` FROM `[settings]` WHERE `name`= 'abusing' AND `name`= 'hacking' AND `name`= 'cracking');
$display = mysql_fetch_object($run); 

When i do

$display->something

It wil assign the SELECT condition

i want to assign the WHERE condition. the value of it.

like i want to do this

$display->WHERE->hacking

how to do this?

Link to comment
Share on other sites

the WHERE condition is the thing that u want to select.

for example i have an table named:[settings]

and it contains:

 

status                  name

on                      functions

off                      hacking

on                      cracking

2                        minimalcharacters

 

$object = SELECT `status` FROM `[settings]` WHERE name =*

now i want to assign the WHERE condition.

so it wil only select the thing thats the name *

for example the name functions:

$object->functions

It wil output for example: on

but that isn't the right syntax

how to do this?

Link to comment
Share on other sites

If you want to select a specific setting, such as the functions setting, you would write the query as -

 

SELECT `status` FROM `[settings]` WHERE name = 'functions'

 

If you want to select all the settings, you would write the query as -

 

SELECT `name`, `status` FROM `[settings]`

 

^^^ This last query returns a row for each setting and it would be your responsibility to retrieve them and store them in the way you want to reference the values. You would typically use an array, such as $settings, and use the names as the array keys and use the status as the array values.

Link to comment
Share on other sites

actually both.

But now whats important for me that I want to fetch it with only using one mysql_query()

 

with the table name:

                                                          [settings]

and it contains the following things:

                                                          status          name

                                                          on                something

                                                          off                some

                                                          on                thing

                                                          20                things

 

By referring the 'name' I want to fetch the status.

I want to be able to do this:

$some = $mysqlthing???(I dont know)

echo "the status of some is:".$some;

I wanted to ask how to assign the WHERE but if there is an other alternative to do this then tell me.

Link to comment
Share on other sites

For getting status of the setting named some. Your query would be

$query = "SELECT `status` FROM `settings` WHERE `name` = 'some'";
$result = mysql_query($query);

$row = mysql_fetch_assoc($query);
echo "the status of some is:" . $row['status'];

Link to comment
Share on other sites

actually both.

But now whats important for me that I want to fetch it with only using one mysql_query()

 

with the table name:

                                                          [settings]

and it contains the following things:

                                                          status          name

                                                          on                something

                                                          off                some

                                                          on                thing

                                                          20                things

 

By referring the 'name' I want to fetch the status.

I want to be able to do this:

$some = $mysqlthing???(I dont know)

echo "the status of some is:".$some;

I wanted to ask how to assign the WHERE but if there is an other alternative to do this then tell me.

I mean:

But now whats important for me that I want to fetch all the statusses with only using one mysql_query()

thus NOT this:

$query = "SELECT `status` FROM `settings` WHERE `name` = 'some'";$result = mysql_query($query);$row = mysql_fetch_assoc($query);echo "the status of some is:" . $row['status'];

$query2 = "SELECT `status` FROM `settings` WHERE `name` = 'things'";$result2 = mysql_query($query);$row2 = mysql_fetch_assoc($query2);echo "the status of some is:" . $row2['status'];

And so on

 

I hope u understand me better now.

Link to comment
Share on other sites

Then remove the where clause and use a while loop to display all the settings

 

$query = "SELECT `name`, `status` FROM `settings`'";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($query))
{
    echo 'The setting <b>' . $row['name'] . '</b> is set to <b>' . $row['status'] . '</b><br />';
}

Link to comment
Share on other sites

I want to fetch specific one status with only using one mysql_query to fetch them all by refering the name.

 

so if possible:

 

i want to do this

echo $name['something'], $name['things'];

 

will echo 'on20'.

 

I don't think this is easy to do with not much mysql api functions in php.

 

and not this:

 

<?php
$query = "SELECT `name`, `status` FROM `settings`'";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($query))
{
if($row['name'] == 'some')
{
echo 'status of some is:'.$row['status'];
}
else
{
    echo 'The setting <b>' . $row['name'] . '</b> is set to <b>' . $row['status'] . '</b><br />';
}
}
?>

 

this will slow down my server until it has found: 'status' of 'some'

 

so:

1 mysql_query will fetch all the statusses of all names and referring the specific name only will echo the status.

thats why i asked if i could assign the where clause/function/condition (don't know what it is).

Link to comment
Share on other sites

so if possible:

 

i want to do this

echo $name['something'], $name['things'];

 

You will have to build the array of settings from the results of the query, Like so

// get all settings from database
$query = "SELECT `name`, `status` FROM `settings`'";
$result = mysql_query($query);

// now add all settings to an array
// we'll call it $settings
while($row = mysql_fetch_assoc($query))
{
    $name   = $row['name'];
    $status = $row['status'];
    
    $setting[$name] = $status;
}

// display the status of the setting named some
echo $setting['something'];

Link to comment
Share on other sites

awesome!

 

 

Self I will never be able to discover that.

 

It's a great interpretation of assigning the WHERE condition.

 

Thank you!

 

btw. if someone does know how to assign (directly from the query!) the where condition I would love to hear that. But I don't think thats possible.

Link to comment
Share on other sites

btw. if someone does know how to assign (directly from the query!) the where condition I would love to hear that. But I don't think thats possible.

By assign do you mean something like this?

$data = $db->select->where('name', $name);

 

Rather than having to type out

$query = "SELECT name from table WHERE name = '$name'";
$result = mysql_query($query);
$data = mysql_fetch_assoc($result);

 

If thats what you mean, then you'll want to look for a third party active record class, like PHP Active Record.

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.