-
Posts
24,563 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
You can mix-and-match object methods and procedural functions. This runs fine $db = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE); // using defined constants $res = $db->query("SELECT COUNT(*) FROM votes") or die($db->error); $row = mysqli_fetch_row($res); echo $res->num_rows; // --> 1 echo $row[0]; // --> 182685 edit: You use the object method or pass the object to the procedural function
- 6 replies
-
- mysqli
- conversion
-
(and 2 more)
Tagged with:
-
No need for cron job $brokers = array ( 'www.broker1.co.uk', 'www.broker2.com', 'www.broker3.com', 'www.broker4.co.uk' ); // or use file() as reply^^ $numbrokers = count($brokers); $dayofyear = date('z'); $todaysURL = 'http://'.$brokers[$dayofyear%$numbrokers];
-
<form action ="taupo.php" method="post" name="form1"> <p><input type="checkbox" name='agree[]' /> Auckland</p> </form> <form action ="hamilton.php" method="post" name="form2"> <p><input type="checkbox" name='agree[]' /> North Shore</p> </form> <--- Your 2nd form ends here! <p><input type="checkbox" name='agree[]' /> Waikato</p> Anything below is not in a form <p><input type="checkbox" name="agree[]" /> Taranaki</p> <p><input type="checkbox" name="agree[]" /> Taupo</p> <p><input type="checkbox" name="agree[]" /> Wellington/p> <p><input type="checkbox" name="agree[]" /> Bay of Plenty</p> <p><input type="checkbox" name="agree[]" /> Manukau</p> <p><input type="submit" value="submit" onclick="form2.submit();"/></p> </div> </form> Your first form has no submit button
-
I need to know tomorrow's lottery numbers. Anybody else got a wish list?
-
selecting/searching rows of a Table based on the value of a field in the table.
Barand replied to ajoo's topic in MySQL Help
Simples! $sql = "SELECT COUNT(*) as numcount FROM clubvisit cv WHERE last_visit > ( SELECT MAX(last_visit) FROM clubvisit WHERE points <> ( SELECT points as lastpoints FROM clubvisit JOIN ( SELECT MAX(last_visit) as last_visit FROM clubvisit ) as latest USING (last_visit) ) )";- 22 replies
-
- ajoo
- conditional search query
-
(and 1 more)
Tagged with:
-
:code_tags:
-
selecting/searching rows of a Table based on the value of a field in the table.
Barand replied to ajoo's topic in MySQL Help
$result = mysqli_query($link,$sql) ; $row = mysqli_fetch_assoc($result); echo $row['numcount']; That will give a count of the visits where the points not equal 6 (ie 2). Is that what you now want?- 22 replies
-
- ajoo
- conditional search query
-
(and 1 more)
Tagged with:
-
functions within functions and database connect issue
Barand replied to ajoo's topic in PHP Coding Help
Typically, connection to a database takes far longer than the query so you only want to connect once per page. Store the result from mysqli_connect() in a variable (your connection object) and pass it as a parameter to those functions that need it.- 1 reply
-
- ajoo
- mysqli_connect
-
(and 1 more)
Tagged with:
-
fetch_assoc() does exist, but it is a result object method, not a database object method. $result = $db->query($sqlStr); // returns result object; you can use fetch_assoc with the result object $row = $result->fetch_assoc(); And why would he want to add those deprecated functions of yours when he is using mysqli ?
-
what is the name of the file that the code is in?
-
surely you must have got an error message from mysql_error()?
-
Need help on my my php code. Need to convert Metric Conversion?
Barand replied to love19801's topic in PHP Coding Help
I think it will work better if you switch those constants round. $pounds = $kilo*2.20462; $kilo = $pounds*0.453592; -
perhaps $str = "<Data> <Output> <Hour>1</Hour> <Energy>0</Energy> </Output> <Output> <Hour>1</Hour> </Output> </Data>"; $xml = simplexml_load_string($str); foreach ($xml->Output as $op) { $hr = isset($op->Hour) ? $op->Hour : 0; $en = isset($op->Energy) ? $op->Energy : 0; $ratio = floatval($hr) && floatval($en) ? $en/$hr : 0; echo $ratio.'<br>'; }
-
You could use $a = intval($a); first, which will give 0 if $a is "N/A"; You also need to check if $b==0 or you will get "division by zero" error
-
A INNER JOIN B selects all records with a matching value in A and B A LEFT JOIN B selects all records from A joined to data from B where there is a match. (columns selected from B are null if there is no match) Sounds like you need the LEFT JOIN
-
selecting/searching rows of a Table based on the value of a field in the table.
Barand replied to ajoo's topic in MySQL Help
As I said earlier so here is the anatomy of the query SELECT cv.day_id, cv.dues, cv.last_visit, cv.points FROM clubvisit cv WHERE last_visit > ( SELECT MAX(last_visit) FROM clubvisit --+ WHERE points <> | ( | find the SELECT points as lastpoints --+ | latest date FROM clubvisit | find points | that had a JOIN | value from | point value ( | the record that | not equal to SELECT MAX(last_visit) as last_visit --+ get | matches the | points value FROM clubvisit | latest | latest date in | found in the ) --+ date | the subquery by | latest record as latest USING (last_visit) --+ JOIN on the date --+ ) )- 22 replies
-
- ajoo
- conditional search query
-
(and 1 more)
Tagged with:
-
try SELECT * FROM ( SELECT whatever FROM photos WHERE approved=1 AND cat IN (501,511,502,503,562,504,506,505,508,2,510) AND date BETWEEN CURDATE() - INTERVAL 7 DAY AND CURDATE() ) as thisweek ORDER BY RAND() LIMIT 10;
-
My usual method is this (not valid code - just to give the idea) $where = array(); $whereclause = ''; if (isset(condition1) && !empty(condition1)) { $where[] = "(col1 = 'condition1')"; } if (isset(condition2) && !empty(condition2)) { $where[] = "(col2 = 'condition2')"; } // etc if (count($where) > 0) $whereclause = "WHERE " . join(' AND ', $where); $sql = "SELECT foo, bar $whereclause"; Remember to sanitize conditions
-
Can Somebody Tell Me What 'Overhead' Is In My SQL Database?
Barand replied to ben_1uk's topic in MySQL Help
When performance degradation becomes noticeable and unacceptable. -
Can Somebody Tell Me What 'Overhead' Is In My SQL Database?
Barand replied to ben_1uk's topic in MySQL Help
I'd say the time to optimize is when it begins to have a significant negative impact on performance. -
selecting/searching rows of a Table based on the value of a field in the table.
Barand replied to ajoo's topic in MySQL Help
This is a problem that depends on dates of visits to find the last and consecutive visits. Unfortunately, those date formats are not suitable for date storage in a database. If the last visit in this case were 10/01 following the 14/12 visit there is no way of knowing that that should be the last when sorted by date. (Do not rely on id sequences, use date/times when sequence is an issue). Store dates in yyyy-mm-dd format which is sortable. So the problem is to find the points in the latest and find the latest date before that where the point value is different. Then retrieve the records since that date. <?php $db = new mysqli(HOST, USERNAME, PASSWORD, DATABASE); $db->query("DROP TABLE IF EXISTS clubvisit"); $sql = "CREATE TABLE clubvisit ( day_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, dues INT, last_visit DATE, points INT )"; $db->query($sql); $sql = "INSERT INTO clubvisit VALUES (1 , 900 , '2012-12-01' , 6), (2 , 700 , '2012-12-04' , 7), (3 , 600 , '2012-12-07' , 5), (4 , 600 , '2012-12-09' , 6), (5 , 600 , '2012-12-10' , 6), (6 , 600 , '2012-12-14' , 6)"; $db->query($sql); $sql = "SELECT cv.day_id, cv.dues, cv.last_visit, cv.points FROM clubvisit cv WHERE last_visit > ( SELECT MAX(last_visit) FROM clubvisit WHERE points <> ( SELECT points as lastpoints FROM clubvisit JOIN ( SELECT MAX(last_visit) as last_visit FROM clubvisit ) as latest USING (last_visit) ) )"; ?> RESULTS: +--------+------+------------+--------+ | day_id | dues | last_visit | points | +--------+------+------------+--------+ | 4 | 600 | 2012-12-09 | 6 | | 5 | 600 | 2012-12-10 | 6 | | 6 | 600 | 2012-12-14 | 6 | +--------+------+------------+--------+- 22 replies
-
- 1
-
- ajoo
- conditional search query
-
(and 1 more)
Tagged with:
-
Replace <textpoint name="max_point_cont"></textpoint><br /><br /> <textpoint name="points_earn_cont"></textpoint><br /><br />with <input type="text" name="max_point_cont" value="" /> <input type="text" name="points_earn_cont" value="" /> The values will be in $_POST['max_point_cont'] and $_POST['points_earn_cont']
-
Another method is use hidden fields with same names as the checkboxes defined before the checkboxes. Checked boxes submitted override the hidden values example <?php echo '<pre>',print_r($_POST['cbox'], true),'</pre>'; ?> <form method="post"> <input type="hidden" name="cbox[0]" value="0"> <input type="hidden" name="cbox[1]" value="0"> <input type="hidden" name="cbox[2]" value="0"> <input type="hidden" name="cbox[3]" value="0"> <input type="hidden" name="cbox[4]" value="0"> <input type="checkbox" name="cbox[0]" value="1"> 1<br> <input type="checkbox" name="cbox[1]" value="2"> 2<br> <input type="checkbox" name="cbox[2]" value="3"> 3<br> <input type="checkbox" name="cbox[3]" value="4"> 4<br> <input type="checkbox" name="cbox[4]" value="5"> 5<br> <input type="submit" name="btn" value="Submit"> </form>
-
Well, given that this is a PHP site, guess
-
trying to remove blank lines generated in my program
Barand replied to reptile's topic in PHP Coding Help
I was sure that one (#11) would work. I set up a small test page <html> <head> <script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script> <script type="text/javascript"> $( document ).ready(function() { removeBlanks(); }) function removeBlanks() { var size = document.form1.sel1.options.length; for (var i=0; i<size; i++) { if (document.forms[0].sel1.options[i].text=='') { document.forms[0].sel1.options[i] = null; } } } </script> </head> <body> <form name='form1'> <select name='sel1'> <option value='1'></option> <option value='2'>2</option> <option value='3'></option> <option value='4'>4</option> </select> </form> </body> </html> Note options 1 and 3 have no text to appear in the dropdown. After loading and the removeBlanks() function has run, Firebug shows this, with the blanks removed <body> <form name="form1"> <select name="sel1"> <option value="2">2</option> <option value="4">4</option> </select> </form> </body>