473,786 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Errors while querying a table and using <option></option>...

60 New Member
Here is config.php:
[PHP]<?php //The vars are all ok.
$dbhost = 'localhost';
$dbname = 'forum';
$dbuser = 'toor';
$dbpass = '';
?>[/PHP]

Here is a part of functions.php:
[PHP]<? require "config.php "; //It is in the same directory.
...
function num(){
$start = mysql_connect($ dbhost, $dbuser);
mysql_select_db ($dbname);
$d = mysql_query("SE LECT id FROM atable;");
$c = mysql_num_rows( $d);
mysql_close($st art);
return $c;
}

function id($aa){
$start = mysql_connect($ dbhost, $dbuser);
mysql_select_db ($dbname);
$d = mysql_query("SE LECT id FROM atable;");
$zxc = mysql_fetch_row ($d);
mysql_close($st art);
return $zxc[$aa];
}
...
?>[/PHP]

Part of index.php:
[PHP]<?
...
$cx = num();
for($zp=0;$zp<= $cx;$zp++){
$office = id($zp);

echo "<option>";
echo $office;
echo "</option>";
}
...
?>[/PHP]

There are 13 id in atable.

The problems:
1- When i use the variables from config.php, both functions return null values

2- When i do not use tha vars from config.php, the num() function returns the correct value. But id() only returns the first id from atable.

Can anyone helpme ?????
Aug 9 '07 #1
4 1563
pbmods
5,821 Recognized Expert Expert
Heya, pplers.

When you attempt to connect to MySQL without entering any credentials, PHP will attempt to use the default login info in your php.ini file. Since this is usually also blank, this means that MySQL is configured on your machine to let just about anybody log in without a password!

Have a look at this article.

In terms of the 1 id instead of 13, you're only fetching one row in functions.php.

Try doing this instead:
Expand|Select|Wrap|Line Numbers
  1. $zxc = array();
  2. while($row = mysql_fetch_row($d))
  3. {
  4.     $zxc[] = $row;
  5. }
  6.  
Aug 9 '07 #2
vituko
48 New Member
And the 1 question :

Inside the function body you have to "import" the global variables (included in your file : in the global scope) into the local scope to get access :

global $variable ;

It's a strange behavior of php...
Aug 9 '07 #3
pplers
60 New Member
Thanks the query went great after modifiying something else. But the other problem still remains. When i change the user and/or the dbname for the vars in config.php then, null values are returned... Why is this ????


p.s: Still happening after doing: global $dbuser; etc, etc.

p.s2: global $whatever has to be used inside every function right ??? Isnt there a way to use it at the begining of the code ???
Aug 9 '07 #4
pbmods
5,821 Recognized Expert Expert
Heya pplers.

Try this instead:
config.php:
Expand|Select|Wrap|Line Numbers
  1. if(! ($GLOBALS['db'] = mysql_connect( ... )))
  2. {
  3.     throw new Exception('Unable to connect to database server.');
  4. }
  5.  
  6. if(! (mysql_select_db(' ... ', $GLOBALS['db'])))
  7. {
  8.     throw new Exception('Unable to select database.');
  9. }
  10.  
In other words, actually connect to the database in your config.php file. It's generally the sort of thing that you only need to do once per page load anyway.


The way the 'global' keyword works:
It's actually an alias for $var =& $GLOBALS['var'];

So this would work:
Expand|Select|Wrap|Line Numbers
  1. $GLOBALS['db'] = mysql_connect( ... );
  2.  
  3. function doSomething()
  4. {
  5.     global $db;  // $db =& $GLOBALS['db'];
  6. }
  7.  
Whereas this only works sometimes (depends on the value of the register_global s directive):
Expand|Select|Wrap|Line Numbers
  1. $db = mysql_connect( ... );
  2.  
  3. function doSomething()
  4. {
  5.     global $db;  // $db =& ...?
  6. }
  7.  
Aug 9 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
2577
by: Bart Van der Donck | last post by:
Hello, My hair falls out! Is it possible to retrieve the "ordering value" of the <option> that was clicked inside a range of options ? In other words, with something like <option value="myvalue">mytext</option> is it possible to know the position of that <option> between the other <option>s and retrieve that value through javascript ?
1
2418
by: Ang Talunin | last post by:
Hey, I wondering if it's possible to retrieve all the <option>-fields from a <select> when posting a <form> to a php file Example: I've got a form like this: <form action = phpfile.php method=post > <select name= "name">
6
13024
by: Chris Fink | last post by:
Does anyone know it is possible to include a small image(.gif .jpeg) within a <SELECT><option> so that the user would see the option text as well as a little image(icon) in the option? I know this is not an ASP.NET related question, but I know this group is knowledgeable and quick with responses. Thanks
6
19615
by: joseph.lindley | last post by:
Forgive me for I am a bit of a web-dev novice - but I'm not doing too bad. I'm currently working with a bit of javascript to dynamically add <option>s into a select box. My code currently works fine in Internet Explorer, however in Firefox the dropdown only displays the first option in the list, and when clicked the other values aren't displayed. Here is the code;
1
1870
by: frey | last post by:
i tried to create a dropdown menu and use the option as a control to change content inside another text area the code is like this: <select name="xxxx"><option onclick="changeunitprice(29.87)" value="1744"/> the function changeunitprice() is called when an option is selected
5
1880
by: bb nicole | last post by:
Below is the list menu of search engine.. How to code if i want to put <option selected>ALL</option> Interface <tr> <td>Job Category:</td> <td><select name="jobCategory"> <option selected></option>
4
39420
by: Man-wai Chang | last post by:
-- iTech Consulting Co., Ltd. Expert of ePOS solutions Website: http://www.itech.com.hk (IE only) Tel: (852)2325 3883 Fax: (852)2325 8288
7
6767
by: Shrek | last post by:
I have a drop down on a web page and want to change the cursor from default to pointer, so my style definition has style ="cursor: pointer;" the drop down though fails to change from the default curor. I've tryed applying the style to the <select> tag and to the <option> tags to no avail.
14
9265
by: The Natural Philosopher | last post by:
This is a nasty one and I can't see my way out of it. I have a bunch of select statements in a form, and each select statement has an onchange="do_something(this)" in it, and this works nicely..except when there is only ONE OPTION in a given select. It seems you cannot 'onchange' a single option! Well, that is reasonable. The trouble is I have no way of selecting it since the form itself is a
0
10360
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.