I'm a designer and not developer, so I don't even know HOW to ask this question.
I have a select box in my script and it shows only one user type. But I need it to show more user types, for example, user_type 2, but Ive tried adding AND / OR and it doesnt work.
Heres the script code.
$result = mysql_query ('SELECT id,email,username FROM ' . $dbacct . '
WHERE user_type="3" ORDER BY username ASC', $link) or die(mysql_error());
Heres the full line
<select class=\'widtha\' name=\'contact_user\'>
';
$result = mysql_query ('SELECT id,email,username FROM ' . $dbacct . ' WHERE user_type IN ("2","3") ORDER BY username ASC', $link) or die(mysql_error());
while ($row = mysql_fetch_array ($result))
{
$row = safe_data ($row, 'display');
echo '<option value=\'' . $row['email'] . '\'';
if ($row[id] == $_POST[contact_user])
{
echo ' selected=\'selected\'';
}
echo '> ' . $row['username'] . '</option>';
}
echo '</select>
Please can someone help me.
Anton
$result = mysql_query ('SELECT id,email,username FROM ' . $dbacct . '
WHERE user_type IN ("1","2","3",....) ORDER BY username ASC', $link) or die(mysql_error());
Related
I want to display the last 3 news messages onto my PHP page. For that I am using the following code:
function news($number) {
$number = (int)$number
$query = mysql_query("SELECT `id`, `title`, `author`, `message`, `date`
FROM `news`
WHERE `hidden` = 0
ORDER BY `date`
DESC LIMIT $number");
while ($row = mysql_fetch_array($query))
{
return '<p class="p_sub">' . $row['title'] . '~' . $row['author'] .
'</p><p>' . $row['message'] . '</p>';
var_dump($row);
}
}
echo news(3);
However, this only displays one message, not three. Anyone who can figure out why?
Using return will exit your while loop. What you could do is concatenate a string containing your HTML like so
$html = '';
while ($row = mysql_fetch_array($query)) {
$html .= '<p class="p_sub">' . $row['title'] . '~' . $row['author'] . '</p><p>' . $row['message'] . '</p>';
}
return $html;
My table 'viewlevels' has the following data (among other):
id |title
10 |Cenas
I'm running the SQL query:
SELECT title FROM viewlevels WHERE id=10
Which is returning "Cenas" as expected.
But using the following PHP script, I just get "texto= " , why?
$res = $db->query("SELECT title FROM viewlevels WHERE id=10");
$res->data_seek(0);
while ($row = $res->fetch_assoc()) {
echo " texto= " . $row['title'] . "\n";
};
To see both fields you have to echo those columns:
while ($row = $res->fetch_assoc()) {
echo " id= " . $row['id'] . "\n";
echo " texto= " . $row['title'] . "\n";
};
You don't need to use data_seek in this instance.
$res = $db->query("SELECT title FROM viewlevels WHERE id=10");
while ($row = $res->fetch_assoc()) {
echo " texto= " . $row['title'] . "\n";
}
Will work.
I try to create a drop-down list from a database that contains the main items and their child items. I want the main items to become optgroup labels and the child items the option items.
<select>
<optgroup label="$rs_pa['Name']">
<option value="$rs_ch['Link']">$rs_ch['Name']</option>
<option value="$rs_ch['Link']">$rs_ch['Name']</option>
</optgroup>
<optgroup label="$rs_pa['Name']">
<option value="$rs_ch['Link']">$rs_ch['Name']</option>
<option value="$rs_ch['Link']">$rs_ch['Name']</option>
</optgroup>
</select>
The above is just a scheme to show where each variable should fit..
$rs_pa['Name] - is the name of the parent item.
$rs_ch['Name] - is the name of the child item.
$rs_ch['Link'] - is the link of the child item.
This is the code I have right now:
$mysql_table_items = "sys_menu_top";
$mysql_table_options = "sys_options";
// SQL QUERY
$mysql_result_number = mysql_query("SELECT VALUE FROM $mysql_table_options WHERE Name LIKE 'nav_menu_elements_on_line_usr'") or die($myQuery . "<br/><br/>" . mysql_error());
$mysql_select_parent = "SELECT * FROM $mysql_table_items WHERE `Parent` = '0' AND Type LIKE 'top' AND Active = 1 ORDER BY `Order` ASC";
$mysql_select_child = "SELECT * FROM $mysql_table_items WHERE `Parent` = '" . $rs_pa['ID'] . "' AND Active = 1 ORDER BY `Order` ASC";
// SQL RESULT
$mysql_result_number = mysql_fetch_array($mysql_result_number, MYSQL_ASSOC) or die("Error: Query Failed! " . mysql_error());
$mysql_result_parent = mysql_query($mysql_select_parent) or die("Error: Query Failed! " . mysql_error());
$mysql_result_child = mysql_query($mysql_select_child) or die("Error: Query Failed! " . mysql_error());
$output .= "<select>";
while ($rs_pa = mysql_fetch_array($mysql_result_parent)) {
$output .= "<optgroup label='" . $rs_pa['Name'] . "'>";
while ($rs_ch = mysql_fetch_array($mysql_result_child)) {
$output .= "<option value='" . $rs_ch['Link'] . "'>" . $rs_ch['Name'] . "</option>";
}
$output .= "</optgroup>";
}
$output .= "</select>";
echo $output;
The prblem is that this doesn't show right.. I tried everything but I am not so good with php.. Thank you for any help..
You are referencing to $rs_pa['ID'] on this line:
$mysql_select_child = "SELECT * FROM $mysql_table_items WHERE `Parent` = '" . $rs_pa['ID'] . "' AND Active = 1 ORDER BY `Order` ASC";
Are you sure $rs_pa['ID'] is defined at that point?
Something like this should work better:
$mysql_table_items = "sys_menu_top";
$mysql_table_options = "sys_options";
// SQL QUERY
$mysql_result_number = mysql_query("SELECT VALUE FROM $mysql_table_options WHERE Name LIKE 'nav_menu_elements_on_line_usr'") or die($myQuery . "<br/><br/>" . mysql_error());
$mysql_select_parent = "SELECT * FROM $mysql_table_items WHERE `Parent` = '0' AND Type LIKE 'top' AND Active = 1 ORDER BY `Order` ASC";
// SQL RESULT
$mysql_result_number = mysql_fetch_array($mysql_result_number, MYSQL_ASSOC) or die("Error: Query Failed! " . mysql_error());
$mysql_result_parent = mysql_query($mysql_select_parent) or die("Error: Query Failed! " . mysql_error());
$output = "<select>";
while ($rs_pa = mysql_fetch_array($mysql_result_parent))
{
$output .= "<optgroup label='" . $rs_pa['Name'] . "'>";
$mysql_select_child = "SELECT * FROM $mysql_table_items WHERE `Parent` = '" . $rs_pa['ID'] . "' AND Active = 1 ORDER BY `Order` ASC";
$mysql_result_child = mysql_query($mysql_select_child) or die("Error: Query Failed! " . mysql_error());
while ($rs_ch = mysql_fetch_array($mysql_result_child))
{
$output .= "<option value='" . $rs_ch['Link'] . "'>" . $rs_ch['Name'] . "</option>";
}
$output .= "</optgroup>";
}
$output .= "</select>";
echo $output;
Some further improvements could be made:
Stop using mysql_query and switch to PDO (http://php.net/manual/en/ref.pdo-mysql.php) which is more secure. The mysql_query functions are deprecated starting from PHP 5.5.0 anyway.
Rewrite your query to fetch all the data you need in a single query. This will really speed up things.
Separate your PHP from your HTML by using templates.
I am trying to make an advanced search engine, one in which you can search by first name, last name, zip, city, state, phone, cell phone and email.
I have managed to get it to search by first name, but you have to type the first name correctly as with anything else, I took out everything else but the first name search to find my problem yet, I have yet to find it, Here is a MySQL version of my search code that I am trying to convert to PDO.
MySQL:
<?php
//This is only displayed if they have submitted the form
if ($searching =="yes") {
echo "<h2>Results</h2><p>";
//If they did not enter a search term we give them an error
if ($find == "")
if ($f == "")
if ($info == "")
if ($zip == "")
if ($state == "")
if ($email == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
// Otherwise we connect to our Database
mysql_connect("xxx", "xxxx", "xxx") or die(mysql_error());
mysql_select_db("xxxx") or die(mysql_error());
// We preform a bit of filtering
//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM users WHERE fname
LIKE '%" . mysql_real_escape_string($find) . "%' AND lname
LIKE '%" . mysql_real_escape_string($f) . "%' AND info
LIKE '%" . mysql_real_escape_string($info) . "%' AND zip
LIKE '%" . mysql_real_escape_string($zip) . "%' AND state
LIKE '%" . mysql_real_escape_string($state) . "%' AND email
LIKE '%" . mysql_real_escape_string($city) . "%' AND city
LIKE '%" . mysql_real_escape_string($email) . "%'");
?>
<?php
//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo "<hr><br>First Name: ";
echo $result['fname'];
echo "<br>Last Name: ";
echo $result['lname'];
echo "<br>Home Phone: ";
echo $result['info'];
echo "<br>Cell Phone: ";
echo $result['cp'];
echo "<br>City: ";
echo $result['city'];
echo "<br>State: ";
echo $result['state'];
echo "<br>Zip: ";
echo $result['zip'];
echo "<br>Email: ";
echo $result['email'];
echo "<br><hr>";
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:
</b> " .$find;
}
?>
Now here is my PDO version:
<?
$dsn = 'mysql:host=xxx;dbname=xxx;charset=utf8';
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO($dsn,'xxx','xxx', $opt);
$stmt = $pdo->prepare("SELECT * FROM users WHERE fname= ?");
if ($stmt->execute(array($fname)));
while ($row = $stmt->fetch()) {
print $row['fname'] . "<br>";
print $row['lname'] . "\t<br>";
print $row['info'] . "\n<br>";
print $row['cp'] . "\n<br>";
print $row['state'] . "\n<br>";
print $row['city'] . "\n<br>";
print $row['zip'] . "\n<br>";
print $row['email'] . "\n<br>";
}
?>
Tips, advice, and comments are welcome and appreciated.
I guess you would build a sql-string and use parameters.
You would not include into your sql fields that the user left empty.
// **EDIT** check if there's any user-input...
if (!isset($_POST['fname']) && !isset($_POST['lname'])) { // add all your input-fields
echo "<p>please enter a search term!</p>";
echo 'try again";
exit();
}
$sql = '';
$bind = array();
if (strlen($_POST[‘firstname‘]) > 0) { // assuming your form-method is "post"
$sql .= 'AND fname LIKE :fname ';
$bind['fname'] = '%'.$_POST['firstname'].'%';
}
if (strlen($_POST['lastname']) > 0)
$sql .= 'AND lname LIKE :lname ';
$bind['lname'] = '%'.$_POST['lastname'].'%';
}
// ...
// do this will all of your input-fields...
$sql = 'SELECT * FROM users WHERE ' . trim($sql, 'AND') . ' ORDER BY lname';
$stmt = $pdo->prepare($sql);
$stmt->execute(array($bind));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
// go ahead and display your results with foreach...
Your biggest problem is you’re using AND instead of OR in your WHERE clause. Change it to this:
SELECT *
FROM `table`
WHERE `name` LIKE '%string%'
OR `zip` LIKE '%string%'
// AND SO ON
$query = "SELECT * FROM `status_info_private` WHERE `id`=$id ORDER BY `Status_Date` DESC LIMIT 100";
if ($query_run = mysql_query($query)) {
while ($rows = mysql_fetch_array($query_run)) {
echo '<font color="#009900" > ' . $rows['Name'] . ' ' . ' Says :' . '</font><br/>';
echo '<p align="justify> ' . $rows['Private_status'] . '<br/>';
echo '<p align="right">' . $rows['Status_Date'] . '<br/>';
$like = $rows['Like'];
$unlike = $rows['Unlike'];
}
}
I think everything is correct in the piece of code. But still I am unable to get the output under the column titled as "Private_status". The above code is producing everything correctly except the message under cols "Private_status". I have already checked the spelling of the col name & there is no error in that part.
So, Please tell me what exactly is missing ?
first close your <p> tags and then do a print_r to check what is in $rows
..
Also, start using PDO or mysqli
$query = "SELECT * FROM `status_info_private` WHERE `id`=$id ORDER BY `Status_Date` DESC LIMIT 100";
if ($query_run = mysql_query($query)) {
while ($rows = mysql_fetch_array($query_run)) {
echo '<a href="view_profile.php?id=' . $id . '" color="#009900" > ' . $rows['Name'] . ' ' . ' Says :' . '</a><br/>';
echo '<p align="justify"> ' . $rows['Private_status'] . '</p>';
echo '<p align="right">' . $rows['Status_Date'] . '</p>';
$like = $rows['Like'];
$unlike = $rows['Unlike'];
}
}